Reputation:
I have a table with thousands of rows. There are no ids and so on.
Within the tds of the rows I have a link calling a function and passing "this" to get the link object.
Using jQuery it is easy to get the the closest tr and table (and so the tables.rows.length)
Another table with rows
The rows have mixing className in no structured order tr1 tr2, tr4 maybe clsA, tr3 clsB
and between them are non "class-named" trs or some called separator
-> I want to avoid loops, that's why I ask for some jQuery tricks.
Upvotes: 1
Views: 585
Reputation: 2128
You don't need to use jQuery to get row's index. There's DOM property 'rowIndex' (which is the fastest way to get row index IMO). See more here http://www.w3schools.com/htmldom/prop_tablerow_rowindex.asp
$("#TableId td").click(function()
{
var index = $(this).parent("tr")[0].rowIndex;
alert(index);
});
Sample here: http://jsbin.com/oroje
Upvotes: 2
Reputation: 5221
If you have an id on the table you can use this:
$("#TableId td").click(function()
{
var index = $("#TableId tr").index(this.parent("tr"));
});
Read more about the index method at http://docs.jquery.com/Core/index
Upvotes: 1