Javascript DOM HTML TableHeader Object create

Introduction

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

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

Click the button to create a TH element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, th {
  border: 1px solid black;
}
</style>/*from  w  w w . j av  a 2  s  .co  m*/
</head>
<body>

<table id="myTable">
  <tr id="myTr">
  </tr>
</table>
<button onclick="myFunction()">Test</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>



PreviousNext

Related