Table createTHead() Method - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Table

Description

The createTHead() method creates an empty <thead> element and adds it to the table.

If a <thead> element already exists on the table, the createTHead() method returns the existing one.

Parameters

None

Return Value:

The newly created or an existing <thead> element

The following code shows how to Create a <thead> element with <tr> and <td> element

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
table, td {
    border: 1px solid black;
}
</style>//from  w w w .ja v  a 2  s . c  o m
</head>
<body>

<table id="myTable">
  <tr>
    <td>cell 1</td>
    <td>cell 2</td>
  </tr>
  <tr>
    <td>cell 3</td>
    <td>cell 4</td>
  </tr>
</table>

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

<script>
function myFunction() {
    var table = document.getElementById("myTable");
    var header = table.createTHead();
    var row = header.insertRow(0);
    var cell = row.insertCell(0);
    cell.innerHTML = "<b>This is a table header</b>";
}
</script>

</body>
</html>

Related Tutorials