Javascript DOM HTML TableData cellIndex Property

Introduction

Click on different cells to get their index position:

Click on each td element to alert its position in the table row.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
  border: 1px solid black;
}
</style>/*  ww w  .  j a va  2s  .c om*/
</head>
<body>
<p id="demo"></p>
<table>
  <tr>
    <td onclick="myFunction(this)">Click to show cellIndex</td>
    <td onclick="myFunction(this)">Click to show cellIndex</td>
    <td onclick="myFunction(this)">Click to show cellIndex</td>
    <td onclick="myFunction(this)">Click to show cellIndex</td>
  </tr>
</table>

<script>
function myFunction(x) {
  document.getElementById("demo").innerHTML = "Cell index is: " + x.cellIndex;
}
</script>

</body>
</html>

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

The cellIndex property returns a Number.




PreviousNext

Related