Javascript Reference - HTML DOM TableData headers Property








The headers property sets or gets the value of 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 = tabledataObject.headers

Set the headers property.

tabledataObject.headers=header_ids




Property Values

Value Description
header_ids Set a space-separated list of id's to 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 {<!--from w w w .j a  va 2s.  c o  m-->
    width: 100%;
}

table, th, td {
    border: 1px solid black;
}
</style>
</head>
<body>
<table id="myTable">
 <tr>
   <th id="name">Name</th>
   <th id="email">Email</th>
   <th id="phone">Phone</th>
   <th id="addr">Address</th>
 </tr>
 <tr>
   <td headers="name">tom</td>
   <td headers="email">t@example.com</td>
   <td headers="phone">+12345555</td>
   <td headers="addr">Main St 300, City, State</td>
 </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 <td> element with id "myTd".


<!DOCTYPE html>
<html>
<head>
<style>
table {<!--  w ww.j a  v  a2  s.c  om-->
    width: 100%;
}

table, th, td {
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
 <tr>
   <th id="name">Name</th>
   <th id="email">Email</th>
   <th id="phone">Phone</th>
   <th id="addr">Address</th>
 </tr>
 <tr>
   <td id="myTd" headers="name">tom</td>
   <td headers="email">t@example.com</td>
   <td headers="phone">+12342323</td>
   <td headers="addr">Main St 300, City ,State</td>
 </tr>
</table>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    document.getElementById("myTd").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 <td> element with id "myTd".


<!DOCTYPE html>
<html>
<head>
<style>
table {<!--   w  ww . j av a 2 s .  com-->
    width: 100%;
}

table, th, td {
    border: 1px solid black;
}
</style>
</head>
<body>
<table>
 <tr>
   <th id="name">Name</th>
   <th id="email">Email</th>
   <th id="phone">Phone</th>
   <th id="addr">Address</th>
 </tr>
 <tr>
   <td id="myTd" headers="name">tom</td>
   <td headers="email">t@example.com</td>
   <td headers="phone">+12342323</td>
   <td headers="addr">Main Street 0, City,Province</td>
 </tr>
</table>
<br>

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

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

<script>
function myFunction() {
    var x = document.getElementById("myTd").headers;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: