Jquery find nearest matching element

imperium2335 picture imperium2335 · Aug 1, 2012 · Viewed 171.5k times · Source

I have a series of rows with columns and I want to select the value of an input field that is in a previous column to the input field (price input) that I am calling a function on when a key is released.

I have tried:

quantity = $(this).parent().parent().children().val() ;
quantity = $(this).parent().parent().children().closest('.inputQty', this).val() ;

But neither work.

An example of the DOM:

<div class="row">
    <div class="column"><input class="inputQty" id="quantity0" /></div>
    <div class="column"><input class="someOther" id="Other0" /></div>
    <div class="column">
        <div class="cSelect">
            <select id="currency0"><option>£</option></select>
            <input class="price" id="price0" />
        </div>
    </div>
</div>

Answer

Mitya picture Mitya · Aug 1, 2012
var otherInput = $(this).closest('.row').find('.inputQty');

That goes up to a row level, then back down to .inputQty.