html file:
<div id ="main">
</div>
Javascript:
//create a div
var divElem = $('<div class="divText"></div>');
//create input element inside it
var inhtml = "<input type='text' id='12345' />";
//add to page
$(divElem).html(inhtml).appendTo('#main')
Following does not work as it is unable to find input element:
//retrieve input element
$('#12345').val('hello')
Following works:
document.getElementById('12345').value = 'hello'
Following works:
var ipElem = $(inhtml);
$(divElem).append(ipElem).appendTo('#main')
$(ipElem).val('hello')
Can anyone tell why first version of retrieving element does not work in jquery? (just starting with jquery… 🙂 )
Edit:
I think ‘12345’ works but not some weird id like: mytext0.15942923246580176
See here:
http://jsfiddle.net/9PVNb/4/
You’re wrapping
divElemin jQuery object twice:EDIT:
Your code at http://jsfiddle.net/9PVNb/4/ isn’t working because:
You can make it work by doing this: