JQuery find closest matching element

I have a series of rows with columns, and I want to select the value of the input field, which is in the previous column, in the input field (price input), which I call the function on when the key is.

I tried:

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

But does not work.

DOM example:

 <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> 
+48
jquery dom-traversal
Aug 01 2018-12-12T00:
source share
4 answers
 var otherInput = $(this).closest('.row').find('.inputQty'); 

This rises to line level and then returns.

+103
Aug 01 2018-12-12T00:
source share

closest() only looking for parents, I assume you really want .find()

 $(this).closest('.row').children('.column').find('.inputQty').val(); 
+9
Aug 01 2018-12-12T00: 00Z
source share

Get the parent .column of this element, get its previous sibling, then find any input there:

 $(this).closest(".column").prev().find("input:first").val(); 

Demo: http://jsfiddle.net/aWhtP/

+1
Aug 01 2018-12-12T00: 00Z
source share

You can try:

$(this).closest(".column").prev().find(".inputQty").val();

0
Aug 01 2018-12-12T00: 00Z
source share



All Articles