jqueryjquery-selectorschildren

How can I use jquery to get the value of a child of a child that is in an adjacent tag?


Starting from a particular div I want to use jquery to grab the value of the input tag in the next div block (two levels down). I have an HTML structure like this:

<div class="firstClass1" id="firstID1"> // suppose I start from this element
    <div class="secondClass1" id="secondID1">
        <input value="myValue1"/>
    </div>
</div>
<div class="firstClass2" id="firstID2">
    <div class="secondClass2" id="secondID2">
        <input value="myValue2"/>
    </div>
</div>
<div class="firstClass3" id="firstID3">
    <div class="secondClass3" id="secondID3">
        <input value="myValue3"/>
    </div>
</div>
...

That is, if I start from .firstClass1, I want to get the value from myValue2. I have tried using the following jquery, but it doesn't work:

var nextID = $('.div#firstID1').next().find('input').val();

and

var nextID = $('.div#firstID1').next().next().val();

Solution

  • you have missed a quotation mark, also remove the dot from .div#firstID1:

    var nextID = $('div#firstID').next().find('input').val();