document.getElementsByTagName("table").length
and also use the row and/or cell array to reference a specific row or cell.
document.getElementsByTagName('table')[0].getElementsByTagName('tr')[0].getElementsByTagName('td')[0]
Reference the 1st cell in row 1
| Row 0 Cell 0 or Table Cell 0 |
Row 0 Cell 1 or Table Cell 1 |
| Row 1 Cell 0 or Table Cell 2 |
Row 1 Cell 1 or Table Cell 3 |
| Row 2 Cell 0 or Table Cell 4 |
|
Note that Javascript counts from zero.
The last example referenced a cell by its ordinal number within the table row, the following example references a cell by its ordinal number within the table, cells are counted left to right, top to bottom.
document.getElementsByTagName('table')[0].getElementsByTagName('td')[3]
A more direct method would be to give a table an ID
document.getElementById("tableID")
document.getElementById("tableID").getElementsByTagName("tr")[n]
document.getElementById("tableID").getElementsByTagName("tr")[n].getElementsByTagName("td")[n]
document.getElementById("tableID").getElementsByTagName("td")[n]
Table 2
| Row 1 Cell 1 | Row 1 Cell2 |
| Row 2 Cell 3 | Row 2 Cell 4 |
| Row 3 Cell 5 | Row 3 Cell 6 |
Using document.getElementById("tableID").getElementsByTagName("tr").length shows the number of rows in table 2.
Using document.getElementById("tableID").getElementsByTagName("td").length shows the number of cells in table 2.
Table Row 1 Row 1 Cell 1 Cell 3
| ? 1 ? | ? 2 ? | ? 3 ? |
| ? 4 ? | ? 5 ? | ? 6 ? |
| ? 7 ? | ? 8 ? | ? 9 ? |
<script type="text/javascript">
<!--
moz=document.getElementById&&!document.all
function tableTest(e,cellinfo){
eventSource=(moz?e.target:event.srcElement)
tableRow=eventSource.parentNode.rowIndex
tableCell=eventSource.cellIndex
cellCount=eventSource.cellIndex
for(var i=0;i<tableRow;i++){
cellCount+=eventSource.offsetParent.rows[i].cells.length
}
alert(cellinfo+"\n\nRow Number = "+(tableRow+1)+"\n\nRow Cell Number = "+(tableCell+1)+"\n\nTable Cell Number = "+(cellCount+1) )
}
//-->
</script>
<table border="1" height="150" bordercolor="black" style="cursor:hand;cursor:pointer">
<tbody>
<tr align="center" valign="center">
<td width="50" onclick="tableTest(event,'this.offsetParent.nodeName = '+this.offsetParent.nodeName)">?</td>
<td width="50" onclick="tableTest(event,'this.offsetParent.rows.length = '+this.offsetParent.rows.length)">?</td>
<td width="50" onclick="tableTest(event,'this.offsetParent.cells.length = '+this.offsetParent.cells.length)">?</td>
</tr>
<tr align="center" valign="center">
<td onclick="tableTest(event,'this.parentNode.parentNode.nodeName = '+this.parentNode.parentNode.nodeName)">?</td>
<td onclick="tableTest(event,'this.parentNode.nodeName = '+this.parentNode.nodeName)">?</td>
<td onclick="tableTest(event,'this.parentNode.rowIndex = '+this.parentNode.rowIndex)">?</td>
</tr>
<tr align="center" valign="center">
<td onclick="tableTest(event,'this.parentNode.cells.length = '+this.parentNode.cells.length)">?</td>
<td onclick="tableTest(event,'this.cellIndex = '+this.cellIndex)">?</td>
<td onclick="tableTest(event,'this.offsetParent.cellIndex = '+this.offsetParent.cellIndex)">?</td>
</tr>
</tbody>
</table>
| ? | ? | |
| ? | ? | ? |
| ? | ? | |