Search code examples
jqueryhtml-tableordinals

jquery: ordinal of td inside tr


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());
       // ...
    });