TableHeader cellIndex Property - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableHeader


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 a 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>
table, th {
    border: 1px solid black;
</style>/*www. j a  v a  2s . c  o m*/

<table border="1">
    <th>Cell 1</th>
    <th>Cell 2</th>
    <th>Cell 3</th>
    <th>Cell 4</th>

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

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

function myFunction() {
    var x = document.getElementsByTagName("th");
    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;


Related Tutorials