jquery: ordinal of td inside tr
-
25-09-2019 - |
Question
I have a row in an html-table that contains only images. (this happens to be the first row too). Those images are wired for a click event too. While trying to handle the click event I can find out its parent (i.e. <td>
element). But I want to know its relative ordinal in the table row (<tr
>).
Solution
You can find the ordinal with the index()
function:
$('td img').click(function() {
var ordinal = $(this).closest('tr').children().index($(this).parent());
// ...
});
OTHER TIPS
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 resulting set:
$('td').click(function(){
alert($(this).prevAll().length);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow