JavaScript createTHead() Method Tutorial

In this section, we will learn that the createTHead() method is and how to use it in JavaScript.

What is createTHead() Method in JavaScript?

The JavaScript createTHead() creates and adds a <thead> element for a table.

Note: if the target table already has a <thead> then calling this method on the table won’t create another header anymore! Instead, it will just return a reference from that <thead>.

Also, you can use the deleteTHead() method to delete a <thead> from a table.

Note: when creating a <thead> element, you must also add one or more rows to it as well. To do this, you can use the insertRow() method.

createTHead() Method Syntax:

tableElement.createTHead();

createTHead() Method Parameter

The method does not take an argument.

createTHead() Method Return Value

If the target table already has a <thead> element, then calling this method will return a reference from that element.

Otherwise, if there’s no <thead> element, the return value will be a reference to the new <thead> that is created.

Example: using createTHead() Method in JavaScript

See the Pen using createTHead() Method in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Note that here because there’s a <thead> element already on this table, calling the `createTHead()` method will return just a reference to this element instead of creating a new one.

More to read:

JavaScript insertRow() method

JavaScript insertCell() method

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies