HTMLTableElement
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The HTMLTableElement interface provides special properties and methods (beyond the regular HTMLElement object interface it also has available to it by inheritance) for manipulating the layout and presentation of tables in an HTML document.
Instance properties
Inherits properties from its parent, HTMLElement.
HTMLTableElement.caption-
An
HTMLTableCaptionElementrepresenting the first<caption>element child of the given<table>, ornullif no such element exists. This property can be assigned, which causes the existing first<caption>element child, if any, to be removed, and the given value, if it is notnull, to be inserted as the first child. If the assigned value is not anHTMLTableCaptionElementornull, aTypeErroris thrown. HTMLTableElement.tHead-
An
HTMLTableSectionElementrepresenting the first<thead>element child of the given<table>, ornullif no such element exists. This property can be assigned, which causes the existing first<thead>element child, if any, to be removed, and the given value, if it is notnull, to be inserted immediately before the first element child that's neither a<caption>nor a<colgroup>, or as the last child if there is no such element. If the assigned value is not anHTMLTableSectionElementornull, aTypeErroris thrown; otherwise, if it is not a<thead>element ornull, aHierarchyRequestErrorDOMExceptionis thrown. HTMLTableElement.tFoot-
An
HTMLTableSectionElementrepresenting the first<tfoot>element child of the given<table>, ornullif no such element exists. This property can be assigned, which causes the existing first<tfoot>element child, if any, to be removed, and the given value, if it is notnull, to be inserted as the last child. If the assigned value is not anHTMLTableSectionElementornull, aTypeErroris thrown; otherwise, if it is not a<tfoot>element ornull, aHierarchyRequestErrorDOMExceptionis thrown. HTMLTableElement.rowsRead only-
Returns a live
HTMLCollectionof all<tr>elements that are a child of the given<table>element, or a child of one of the table's<thead>,<tbody>, and<tfoot>children. The members of the<thead>appear first, followed by members of the<tbody>and the table itself, and members of the<tfoot>come last, sorted by tree order within each group. The returned object is automatically updated when theHTMLTableElementchanges. HTMLTableElement.tBodiesRead only-
Returns a live
HTMLCollectionof all<tbody>element children of the given<table>. The returned object is automatically updated when theHTMLTableElementchanges.
Obsolete Properties
Warning: The following properties are obsolete. You should avoid using them.
HTMLTableElement.align-
A string containing an enumerated value reflecting the
alignattribute. It indicates the alignment of the element's contents with respect to the surrounding context. The possible values are"left","right", and"center". HTMLTableElement.bgColor-
A string containing the background color of the cells. It reflects the obsolete
bgColorattribute. HTMLTableElement.border-
A string containing the width in pixels of the border of the table. It reflects the obsolete
borderattribute. HTMLTableElement.cellPadding-
A string containing the width in pixels of the horizontal and vertical space between cell content and cell borders. It reflects the obsolete
cellpaddingattribute. HTMLTableElement.cellSpacing-
A string containing the width in pixels of the horizontal and vertical separation between cells. It reflects the obsolete
cellspacingattribute. HTMLTableElement.frame-
A string containing the type of the external borders of the table. It reflects the obsolete
frameattribute and can take one of the following values:"void","above","below","hsides","vsides","lhs","rhs","box", or"border". HTMLTableElement.rules-
A string containing the type of the internal borders of the table. It reflects the obsolete
rulesattribute and can take one of the following values:"none","groups","rows","cols", or"all". HTMLTableElement.summary-
A string containing a description of the purpose or the structure of the table. It reflects the obsolete
summaryattribute. HTMLTableElement.width-
A string containing the length in pixels or in percentage of the desired width of the entire table. It reflects the obsolete
widthattribute.
Instance methods
Inherits methods from its parent, HTMLElement.
HTMLTableElement.createTHead()-
Creates a
<thead>element, inserts it before the first element child of the given<table>that's neither a<caption>nor a<colgroup>, or as the last child if no such insertion location is found, and returns it. If the table already has a<thead>element child, this method returns the first such child without creating one. HTMLTableElement.deleteTHead()-
Removes the first
<thead>element child from a given<table>, if any. HTMLTableElement.createTFoot()-
Creates a
<tfoot>element, inserts it as the last child of the given<table>, and returns it. If the table already has a<tfoot>element child, this method returns the first such child without creating one. HTMLTableElement.deleteTFoot()-
Removes the first
<tfoot>element child from a given<table>, if any. HTMLTableElement.createTBody()-
Creates a
<tbody>element, inserts it immediately after the last<tbody>element child of the given<table>, or as the last child if there is no such element, and returns it. HTMLTableElement.createCaption()-
Creates a
<caption>element, inserts it as the first child of the given<table>, and returns it. If the table already has a<caption>element child, this method returns the first such child without creating one. HTMLTableElement.deleteCaption()-
Removes the first
<caption>element child from a given<table>, if any. HTMLTableElement.insertRow()-
Creates a
<tr>element, inserts it at the specified position in therowscollection, and returns it. If therowscollection is empty and the table also has no<tbody>elements, a<tbody>element is first created and inserted. HTMLTableElement.deleteRow()-
Removes a specific row (
<tr>) from a given<table>. Ifindexis-1, the last row is removed.
Examples
>Using the DOM Table Interface
The HTMLTableElement interface provides some convenience methods for creating and manipulating tables. Two frequently used methods are HTMLTableElement.insertRow and HTMLTableRowElement.insertCell.
To add a row and some cells to an existing table:
<table id="table0">
<tbody>
<tr>
<td>Row 0 Cell 0</td>
<td>Row 0 Cell 1</td>
</tr>
</tbody>
</table>
const table = document.getElementById("table0");
const row = table.insertRow(-1);
for (let i = 0; i < 2; i++) {
const cell = row.insertCell(-1);
const text = `Row ${row.rowIndex} Cell ${i}`;
cell.appendChild(document.createTextNode(text));
}
Specifications
| Specification |
|---|
| HTML> # htmltableelement> |
Browser compatibility
See also
- The HTML element implementing this interface:
<table>.