The cellIndex
property returns the index
of a cell in the cells collection of a table row.
cellIndex |
Yes | Yes | Yes | Yes | Yes |
Return the cellIndex property.
tableheaderObject.cellIndex
A Number type value representing the position of the cell in the cells collection of a table row.
The following code shows how to click on different cells to output their index position.
<!DOCTYPE html>
<html>
<head>
<style>
table, th {<!--from w w w . j a v a 2 s . c o m-->
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tr>
<th onclick="myFunction(this)">Click to show cellIndex</th>
<th onclick="myFunction(this)">Click to show cellIndex</th>
<th onclick="myFunction(this)">Click to show cellIndex</th>
<th onclick="myFunction(this)">Click to show cellIndex</th>
</tr>
</table>
<script>
function myFunction(x) {
console.log("Cell index is: " + x.cellIndex);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to get the index position of each cell in a table row.
<!DOCTYPE html>
<html>
<head>
<style>
table, th {<!--from www . j ava 2 s. c o m-->
border: 1px solid black;
}
</style>
</head>
<body>
<table border="1">
<tr>
<th>Cell 1</th>
<th>Cell 2</th>
<th>Cell 3</th>
<th>Cell 4</th>
</tr>
</table>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementsByTagName("th");
var i;
for (i = 0; i < x.length; i++) {
console.log("The index of Cell "+(i+1)+" is: "+x[i].cellIndex);
}
}
</script>
</body>
</html>
The code above is rendered as follows: