15

I'm now implementing something like this post (specifically, the effect takes place when the row is clicked)

How do I know what the index of the row is inside the table?

Community
  • 1
  • 1
user198729
  • 61,774
  • 108
  • 250
  • 348

4 Answers4

29

If you defined the click handler directly on the tr elements, you can use the index method like this:

$('#tableId tr').click(function () {
  var rowIndex = $('#tableId tr').index(this); //index relative to the #tableId rows
});

If the click event is bound not directly on the tr element (if you are using an anchor, a button, etc...), you should find the closest tr to get the right index:

$(selector).click(function () {
  var rowIndex = $('#tableId tr').index($(this).closest('tr'));

  return false;
});

Try an example here.

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • This won't work right if the page has multiple tables. Probably best to look for sibling `