Reputation: 1873
I am using an existing jsfiddle found on stackoverflow, which has this div:
<div class='sort-me'>
<li>Lorem<input style="float:right;"></input></li>
<li>ipsum<input style="float:right;"></input></li>
<li>dolor<input style="float:right;"></input></li>
<li>dolor<input style="float:right;"></input></li>
<li>dolor<input style="float:right;"></input></li>
</div>
I am trying to set the text of the input , but cannot get it right:
Examples of what I tried:
$( "li" ).each(function() {
$( this ).childNodes[0].text('x');
$( this ).childNodes[1].text('x');
});
Upvotes: 0
Views: 656
Reputation: 133403
You can use .find()
to travese to input
then use .val()
to set its value.
$(this).find('input').val('x');
To find text node you can use
var textNodeElem = $(this).contents().filter(function() {
return this.nodeType === 3; //Node.TEXT_NODE
});
textNodeElem[0].textContent='XYZ';
Upvotes: 1
Reputation: 115232
You can use the following method , using find()
and val()
methods in jQuery
$("li").each(function() {
$(this).find('input').val('x');
});
Upvotes: 1