Javascript DOM HTML TableHeader headers Property get cell header for second row

Introduction

Display cell headers of second row:

Click the button to return the value of the headers attribute of each th element in second row.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th {
  border: 1px solid black;
}
</style>/*from   w  w  w.j av  a2  s  .c o  m*/
</head>
<body>
<table id="myTable">
  <tr>
    <th id="name" colspan="3">Name</th>
  </tr>
  <tr>
    <th headers="fname">First name</th>
    <th headers="mname">Middle name</th>
    <th headers="lname">Last name</th>
  </tr>
</table>
<br>

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

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

<script>
function myFunction() {
  var table = document.getElementById("myTable");
  var txt = "";
  var i;
  for (i = 0; i < table.rows[1].cells.length; i++) {
    txt = txt + table.rows[1].cells[i].headers + "<br>";
  }
  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>



PreviousNext

Related