stephan
stephan

Reputation:

jquery recognize in which row iam

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)

  1. I want to know as easy in which row I am. OK I could do a loop but does exist any easier possibility?
#

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

  1. I want to know which row comes first clsA or clsB -> remember it is not the first sibling etc. there can be empty trs or separator.

-> I want to avoid loops, that's why I ask for some jQuery tricks.

Upvotes: 1

Views: 585

Answers (2)

algiecas
algiecas

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

bang
bang

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

Related Questions