TableHeader cellIndex Property - Return the index position of each cell in a table row: - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableHeader

Description

TableHeader cellIndex Property - Return the index position of each cell in a table row:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th {
    border: 1px solid black;
}
</style>/*from  w  ww .  j ava 2s.  co m*/
</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 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