Jquery: td sequence number inside tr

I have a row in an html table that contains only images. (this will also be the first line). These images are also connected to the event with a click. When I try to handle the click event, I can find out its parent element (i.e. <td> ). But I want to know its relative serial number in the table row ( <tr >).

+4
source share
2 answers

You can find the serial number with the index() function:

 $('td img').click(function() { var ordinal = $(this).closest('tr').children().index($(this).parent()); // ... }); 
+5
source

The number of elements before the current element will be its index in the DOM relative to the parent. You can use .prevAll() and get the length of the result set:

 $('td').click(function(){ alert($(this).prevAll().length); }); 

Demo available on jsfiddle

+1
source

Source: https://habr.com/ru/post/1312312/


All Articles