Peter PitLock
Peter PitLock

Reputation: 1873

Set Value of Input (textbox) nested in LI

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

Answers (2)

Satpal
Satpal

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

Pranav C Balan
Pranav C Balan

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

Related Questions