faqts : Computers : Programming : Languages : JavaScript : Tables

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

90 of 95 people (95%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I find the column and row index of a table cell?
How can I find the column and row index of a table cell?

Mar 25th, 2000 08:19
Martin Honnen,


With full DOM access (NN6, IE4+) a TD element has a
  cellIndex
property and its parentNode, the TR element has a 
  rowIndex
property.
So a complete example looks like

<HTML>
<HEAD>
<SCRIPT>
function getRowIndex (cell) {
  return document.all ? cell.parentElement.rowIndex :
           cell.parentNode.rowIndex;
}
</SCRIPT>
</HEAD>
<BODY>
<TABLE BORDER="1">
<SCRIPT>
for (var r = 0; r < 3; r++) {
  document.write('<TR>');
  for (var c = 0; c < 4; c++) {
    document.write(
'<TD ONCLICK="alert(getRowIndex(this) + \',\' + this.cellIndex)">');
    document.write(r + ', ' + c + ': ' + 'Kibology');
    document.write('<\/TD>');
  }
  document.write('<\/TR>');
}
</SCRIPT>
</TABLE>
</BODY>
</HTML>