Abhishek Susarla
Abhishek Susarla

Reputation: 578

Get table rowIndex of a HTML table without traversing - Only Javascript

I have a table that automatically adds new rows, once u go to the last cell and tab over.

I click on one of the cells - I want to know the rowIndex of the clicked cell (row)

I havent been able to uniquely identify the cell using any attribute eg classname etc, ID is randomly generated. Name , TagName everything is generic - Same for all rows.

How do I get the rowIndex just using a cell's info

No jquery sols pls - not allowed in my framework

Upvotes: 1

Views: 1619

Answers (2)

dfsq
dfsq

Reputation: 193261

You can grab this info from the parent node tr as rowIndex:

td.parentNode.rowIndex

Here td is HTMLTableCellElement element.

td has a reference to its parent row element parentNode (tr), which in its own turn has a property rowIndex. Similarly td itself has a property cellIndex.

Demo: http://jsfiddle.net/prFSd/1/

Upvotes: 3

Lee Bee
Lee Bee

Reputation: 78

dfsq provides a good answer, but it uses jquery which you said you could not use. You could put this in each row

onclick="myFunction(this)"

then define the function

function myFunction(x)
{
alert("Row index is: " + x.rowIndex);
}

Upvotes: 1

Related Questions