Javascript DOM HTML TableData headers Property get by row id

Introduction

Change the value of the headers attribute of a <td> element with id=myTd:

document.getElementById("myTd").headers = "newValue";

Click the button to change the value of the headers attribute of td with id=myTd.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table {/*w  w  w  .j  a  va 2 s  . c o  m*/
  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">java2s.com</td>
    <td headers="email">someone@example.com</td>
    <td headers="phone">+123456789</td>
    <td headers="addr">PO 1234, Main Street U.S.A.</td>
  </tr>
</table>
<br>

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

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

<script>
function myFunction() {
  document.getElementById("myTd").headers = "newValue";
  document.getElementById("demo").innerHTML = "The value of the headers attribute was changed from 'name' to 'newValue'.";
}
</script>

</body>
</html>



PreviousNext

Related