5

I want to change the text field into a label tag with input user typed text inside it. I have tried and seen many example which are working but not set value as a text in label. e.g.:

<input id="1" type="text" value="hi"/>

I want to replace this input text field with label or div tag with its innerHTML hi or the user typed value i have tried the example below but i it is not done by me.

<p id="1" onclick="wish(id)">sasdsad</p>
<script>    
    document.getElementById("1").outerHTML = document.getElementById("1").outerHTML.replace(/p/g,"div");
</scrip>
6
  • Sorry For Mistake document.getElementById("1").outerHTML = document.getElementById("1").outerHTML.replace(/p/g,"div"); Commented Jul 30, 2015 at 8:07
  • i found this example in jsfiddle.net/s98J5 Commented Jul 30, 2015 at 8:08
  • ID's cannot and should not just be a number, they must start with some text identifier such as <p id="sad1">sadsad</p> Commented Jul 30, 2015 at 8:12
  • yes it is working for the above example but it doesnot work for problem thanks Commented Jul 30, 2015 at 8:14
  • do you want something like this - jsfiddle.net/xwszt2s9 Commented Jul 30, 2015 at 8:20

2 Answers 2

5

Element id should start with character not number.

With using jQuery

<input id="one" type="text" value="hi"/>

$('#one').replaceWith("<div>"+$('#one').val()+"</div>");
Sign up to request clarification or add additional context in comments.

Comments

0

Or you can use like this as well, just for an alternative solution :

html

<input id="toChange" type="text" value="hi"/>
<p id="clickMe" onclick="changeTag()">Click Me</p>

jQuery

function changeTag(){
  var originalForm = $('#toChange');
  var div = $('<div/>',{
               text : originalForm.val(),
               id : originalForm.attr('id')
            });
  $('#toChange').replaceWith(div);
}

DEMO

1 Comment

The link of the demo is down

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.