THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Table createTHead() Method

Table Object Reference Table Object

Example

Create a <thead> element (and insert a <tr> and <td> element to it):

// Find a <table> element with id="myTable":
var table = document.getElementById("myTable");

// Create an empty <thead> element and add it to the table:
var header = table.createTHead();

// Create an empty <tr> element and add it to the first position of <thead>:
var row = header.insertRow(0);    

// Insert a new cell (<td>) at the first position of the "new" <tr> element:
var cell = row.insertCell(0);

// Add some bold text in the new cell:
cell.innerHTML = "<b>This is a table header</b>";
Try it Yourself »

Definition and Usage

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

Note: If a <thead> element already exists on the table, the createTHead() method returns the existing one, and does not create a new one.

Note: The <thead> element must have one or more <tr> tags inside.

Tip: To remove the <thead> element from a table, use the deleteTHead() method.


Browser Support

Method
createTHead() Yes Yes Yes Yes Yes

Syntax

tableObject.createTHead()

Parameters

None

Technical Details

Return Value: The newly created (or an existing) <thead> element

More Examples

Example

Create and delete a <thead> element:

function myCreateFunction() {
    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>";
}

function myDeleteFunction() {
    document.getElementById("myTable").deleteTHead();
}
Try it Yourself »

Related Pages

HTML reference: HTML <thead> tag


Table Object Reference Table Object