JQuery get shortcut on top

I write a function to update the receipt of an order every time the user is updated, and then closes the product popup. The function searches for each input of the form, and then adds information to the receipt of the receipt, if its value is greater than 0 . I am trying to get the .html() of a label element located above the input field that describes the current element and use it as a description of the element in the receipt.

I tried using without success:

  - $this.closest('label').html() - $this.prev('label').html() - $this.find('label').html() - this.element.find('label').html() 

Here is my code. The section I'm talking about is part of the 'label is' ...

 function updateOrder(){ var items = ''; $('input').each(function(){ var $this = $(this), i_value = $this.attr('value'); if(i_value > 0){ items += $this.attr('id') + ' Quantity: ' + i_value + '<br/>' + 'label is: ' + $this.closest('label').html() + '<br/>'; } }); $('#d_reciept').html(items); 

}

and sample shape element

 <tr> <td class="td_thumbs"> <div> <a class="fancybox" data-fancybox-group="vinyl-banners" href="img/products/vinyl-corporateblue.jpg"> <img src="img/products/thumbs/vinyl-corporateblue-thumb.png" alt="vinyl c-blue"/></a> <label>Corporate Blue</label> </div> </td> <td>6</td> <td> <input id="vinyl-blue" type="number" max="6" min="0" value="0"/> </td> </tr> 
+6
source share
1 answer

The closest function gives you the first confirmation of the given selector in ancestors , your label is not in the first ancestors, and not in the parent tr children . You need to go to parent tr and then use find to search for its parent for label .

 $(this).closest('tr').find('label').html() 

or

 $(this).closest('tr :td:eq(0)').find('label').html() 
+16
source

All Articles