Create a TableHeader Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:TableHeader

Introduction

You can create a <th> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th {
    border: 1px solid black;
}
</style>/*from  w w  w  .jav  a2  s .c o  m*/
</head>
<body>

<table id="myTable">
  <tr id="myTr">
  </tr>
</table>

<button onclick="myFunction()">create a TH element</button>

<script>
function myFunction() {
    var x = document.createElement("TH");
    var t = document.createTextNode("table header cell");
    x.appendChild(t);
    document.getElementById("myTr").appendChild(x);
}
</script>

</body>
</html>

Related Tutorials