Search code examples
jqueryinputjquery-selectors

Find next input field of specific class


I have several divs with two input fields each, like this:

<div id="wrapper">
    <div class="content">
        <div class="subcontent">
            <input type="text" class="first" id="first_1" />
            <input type="text" class="second" id="second_1" />
        </div>
    </div>
    <div class="content">
        <div class="subcontent">
            <input type="text" class="first" id="first_2" />
            <input type="text" class="second" id="second_2" />
        </div>
    </div>
</div>

So let's say I have the inputfield with id second_1, and assign some value to it:

$("#second_1").val("Hello");

How can I find the next input field with the class "first"? Keep in mind that I don't know its ID, it's not necessarily first_2.

So in other words, I want to assign a value to first_2 as well, but without knowing its ID.


Solution

  • I think this is what you want.

    var nextFirst = $("#second_1").closest('content').next().find('.first');
    

    Use closest(..).next() to get the next row of inputs.