How to transform text to input? [closed]
How can I create something such as:
- there is some text - I click on it - It tra开发者_开发问答nsforms into an input that contains the text I clicked on.How about something like this
HTML
<div onclick="transform(this)">Some text here</div>
Javascript
function transform(obj)
{
obj.innerHTML = "<input type='text' value='" + obj.innerHTML + "' />";
}
Hope I've understood your question correctly.
<a href="#" onclick="var input = document.createElement('input'); input.setAttribute('value', this.firstChild.nodeValue); this.parentNode.replaceChild(input, this);">here is some text</a>. Click it.
Instead of <a>
you could also use <span>
or <div>
whatever you feel like.
Assuming HTML:
<p id='changeMe'>This is some text</p>
You can use jQuery:
$('#changeMe').click(function(){
$this = $(this)
$this.replaceWith( $('<input />').val( $this.text() ) )
})
See demo here: http://jsfiddle.net/aXZ8e/
<span id="target" onclick="javascript:run();" >This is dummy text</span>
<script type="text/javascript">
function run() {
var span = document.getElementById( "target" );
var text = span.innerHTML;
var input = document.createElement("input");
input.type = "text";
input.value = text;
span.innerHTML = "";
span.appendChild( input );
span.onclick = null;
}
</script>
精彩评论