There is a set of product-section product sections. By button + specific section, you need to get the amount to continue to work with it. But I can't get to the upper field quantity via this . How can I do that?

 $('.product-section .product-quantity .plus').click(function() { var $this = $(this); var quantity = $this.find(".quantity").text().match(/\d+(?:\.\d+)?/g); var basePrice = ""; for (var i = 1; i < quantity.length; i++) { basePrice += quantity[i]; } console.log(basePrice); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="product-section"> <table class="description-table"> <tbody> <tr> <td class="product-quantity"> <div class="quantity buttons_added"> <input type="number" step="1" min="1" max="27" id="num_count" name="quantity" value="1" title="Кол." class="input-text qty text" size="4"> <input type="button" value="+1" id="button_plus" class="plus"> <input type="button" value="-1" id="button_minus" class="minus"> </div> </td> </tr> </tbody> </table> </div> 

  • And just by #num_count you can not access the element? - Visman
  • @Visman is not, it is impossible - in this set of sections all elements with id="num_count" - Vasya
  • Item id must be unique. - Visman
  • @Visman is theoretically yes, but in practice it is not always the case - ask wordpress)) - Bob
  • Ignorance of the laws is no excuse. PS Do as it is right, and not as WP will send. - Visman

2 answers 2

You can walk through the parent tree, specifying the desired element in which the input is located. Then in it already find the required input.

 $('.product-section .product-quantity .plus').click(function() { var $input_amount = $(this).parents('.quantity ').find('input[name="quantity"]'); $input_amount.val(parseInt($input_amount.val(),10)+1); alert($input_amount.val()); }); 
  • Yes, @Vitaliy Shevchenko thanks, but decided easier - through the change property of the text field - Bob
 $(this).closest('.quantity').find(...);