Javascript Reference - HTML DOM TableHeader headers Property








The headers property sets or gets the headers attribute.

The headers attribute specifies a list of header cells containing header information for the current data cell.

Browser Support

headers Yes Yes Yes Yes Yes

Syntax

Return the headers property.

var v = tableheaderObject.headers

Set the headers property.

tableheaderObject.headers=header_ids

Property Values

Value Description
header_ids Set a space-separated list of id's of header cells




Return Value

A String type value containing a space-separated list of header cell identifiers.

Example

The following code shows how to display cell headers of second row.


<!DOCTYPE html>
<html>
<head>
<style>
table, th {<!--from   ww  w  . j ava 2s . co m-->
    border: 1px solid black;
}
</style>
</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>
<script>
function myFunction() {
    var table = document.getElementById("myTable");
    var i;
    for (i = 0; i < table.rows[1].cells.length; i++) {
        console.log(table.rows[1].cells[i].headers)
    }
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the value of the headers attribute of a <th> element with id "myTh".


<!DOCTYPE html>
<html>
<head>
<style>
table, th {<!--from   w w  w.j a va 2s  . c  o m-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
  <tr>
    <th id="name" colspan="3">Name</th>
  </tr>
  <tr>
    <th id="myTh" headers="fname">First name</th>
    <th headers="mname">Middle name</th>
    <th headers="lname">Last name</th>
  </tr>
</table>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTh").headers = "newValue";
}
</script>

</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to get the value of the headers attribute of a <th> element with id "myTh".


<!DOCTYPE html>
<html>
<head>
<style>
table, th {<!-- ww  w  . j ava 2s.  c  o m-->
    border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
  <tr>
    <th id="name" colspan="3">Name</th>
  </tr>
  <tr>
    <th id="myTh" 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 x = document.getElementById("myTh").headers;
    document.getElementById("demo").innerHTML = x;
}
</script>
</body>
</html>

The code above is rendered as follows: