Javascript DOM HTML THead Object create

Introduction

We can create a <thead> element via the document.createElement() method:

var x = document.createElement("THEAD");

Click the button to create a THEAD element and a TR and a TD.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
  border: 1px solid black;
}
</style>/*from   w  ww.  j  a v  a2s . com*/
</head>
<body>

<table id="myTable">
 <tbody>
   <tr>
     <td>Cell 1</td>
   </tr>
   <tr>
     <td>Cell 2</td>
   </tr>
  </tbody>
</table>
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.createElement("THEAD");
  var y = document.createElement("TR");
  var z = document.createElement("TD");
  z.innerHTML = "Header"
  y.appendChild(z);
  x.appendChild(y);
  document.getElementById("myTable").appendChild(x);
}
</script>

</body>
</html>



PreviousNext

Related