TableData cellIndex Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableData

Description

The cellIndex property returns the position of a cell in the cells collection of a table row.

Return Value

A Number, representing the position of the cell in the cells collection of the table row

The following code shows how to Click on different cells to alert their index position:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>//from ww w .j  ava 2 s. co m
</head>
<body>

<table>
  <tr>
    <td>Cell 1</td>
    <td>Cell 2</td>
    <td>Cell 3</td>
    <td>Cell 4</td>
  </tr>
</table>

<p id="demo"></p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
    var x = document.getElementsByTagName("td");
    var txt = "";
    var i;
    for (i = 0; i < x.length; i++) {
        txt = txt + "The index of Cell " + (i + 1) + " is: " + x[i].cellIndex + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>

Related Tutorials