Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know index of a <tr> inside a <table> with jQuery?

Tags:

html

jquery

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?

like image 981
user198729 Avatar asked Dec 07 '09 06:12

user198729


People also ask

How do you find the index of an element in a table in JavaScript?

JavaScript Array findIndex() The findIndex() method executes a function for each array element. The findIndex() method returns the index (position) of the first element that passes a test. The findIndex() method returns -1 if no match is found.

How can get TD value from TR in jquery?

var Something = $(this). closest('tr'). find('td:eq(1)'). text();


1 Answers

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.

like image 114
Christian C. Salvadó Avatar answered Oct 19 '22 12:10

Christian C. Salvadó