Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the parent and nearest element using jQuery

I have a simple form as

<tr>
    <td> <input type="text" name="qty[]" placeholder="qty"/> </td>
    <td> <input type="text" name="price[]" placeholder="price"/> </td>
    <td> <input type="text" name="total[]" placeholder="Total"/> </td>
</tr>

we can have multiple row with the same as above.

What I need is when the User inputs the qty or price the row total needs to update.

What I tried

$('input[name=\'qty[]\']').on('change keyup', function(){
    var qty = $(this).val();
    var price = $(this).parent('tr').find('input[name=\'price[]\']').val();  
});

price is undefined

Or is there an easier way to do it? Please check the Fiddle

UPDATE :

.parent(..) selects the direct parent of each of the elements in the current set of elements. The first argument filters this set. The direct parent of your input element is the td element, not the tr element.

Updated Fiddle

like image 880
epynic Avatar asked Nov 14 '15 10:11

epynic


People also ask

How do I find a specific parent in jQuery?

jQuery parent() Method The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree.

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

What is parent in jQuery?

The parent() is an inbuilt method in jQuery which is used to find the parent element related to the selected element. This parent() method in jQuery traverse a single level up the selected element and return that element. Syntax: $(selector).parent() Here selector is the selected elements whose parent need to find.

How does nearest work in jQuery?

The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on. The DOM tree: This method traverse upwards from the current element, all the way up to the document's root element (<html>), to find the first ancestor of DOM elements.


2 Answers

first of, think about making your html as such:

<tr>
    <td> <input type="number" name="qty[]" placeholder="qty"/> </td>
    <td> <input type="number" name="price[]" placeholder="price"/> </td>
    <td> <input type="number" name="total[]" placeholder="Total"/> </td>
</tr>

Also. As far a javascript goes:

jQuery(document).on("change ,  keyup" , "input[name='qty[]'] , input[name='price[]']" ,function(){
     var parent_element = jQuery(this).closest("tr");
     var qty = jQuery(parent_element).find("input[name='qty[]']").val();
     var price = jQuery(parent_element).find("input[name='price[]']").val();
     if( qty.trim() != "" && price.trim() != "")
      {
        jQuery(parent_element).find("input[name='total[]']").val( parseFloat(qty) *parseFloat(price) );
      }
      else
      {
        jQuery(parent_element).find("input[name='total[]']").val("");
      }
});

EDIT :Better approach, properly taking into account empty fields

like image 51
Elentriel Avatar answered Sep 19 '22 15:09

Elentriel


Just change parent() to parents():

Demo:

https://jsfiddle.net/yqe4kwbz/9/

Citing from https://api.jquery.com/parents/ sbout parents():

Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector. The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree.

like image 31
n-dru Avatar answered Sep 23 '22 15:09

n-dru