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-
A
HTMLTableCaptionElementrepresenting the first<caption>that is a child of the element, ornullif none is found. When set, if the object doesn't represent a<caption>, aDOMExceptionwith theHierarchyRequestErrorname is thrown. If a correct object is given, it is inserted in the tree as the first child of this element and the first<caption>that is a child of this element is removed from the tree, if any. HTMLTableElement.tHead-
A
HTMLTableSectionElementrepresenting the first<thead>that is a child of the element, ornullif none is found. When set, if the object doesn't represent a<thead>, aDOMExceptionwith theHierarchyRequestErrorname is thrown. If a correct object is given, it is inserted in the tree immediately before the first element that is neither a<caption>, nor a<colgroup>, or as the last child if there is no such element, and the first<thead>that is a child of this element is removed from the tree, if any. HTMLTableElement.tFoot-
A
HTMLTableSectionElementrepresenting the first<tfoot>that is a child of the element, ornullif none is found. When set, if the object doesn't represent a<tfoot>, aDOMExceptionwith theHierarchyRequestErrorname is thrown. If a correct object is given, it is inserted in the tree immediately before the first element that is neither a<caption>, a<colgroup>, nor a<thead>, or as the last child if there is no such element, and the first<tfoot>that is a child of this element is removed from the tree, if any. HTMLTableElement.rowsRead only-
Returns a live
HTMLCollectioncontaining all the rows of the element, that is all<tr>that are a child of the element, or a child of one of its<thead>,<tbody>and<tfoot>children. The rows members of a<thead>appear first, in tree order, and those members of a<tbody>last, also in tree order. TheHTMLCollectionis live and is automatically updated when theHTMLTableElementchanges. HTMLTableElement.tBodiesRead only-
Returns a live
HTMLCollectioncontaining all the<tbody>of the element. TheHTMLCollectionis live and is automatically updated when theHTMLTableElementchanges.
Obsolete Properties
Warning: The following properties are obsolete. You should avoid using them.
HTMLTableElement.alignDeprecated-
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.bgColorDeprecated-
A string containing the background color of the cells. It reflects the obsolete
bgColorattribute. HTMLTableElement.borderDeprecated-
A string containing the width in pixels of the border of the table. It reflects the obsolete
borderattribute. HTMLTableElement.cellPaddingDeprecated-
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.cellSpacingDeprecated-
A string containing the width in pixels of the horizontal and vertical separation between cells. It reflects the obsolete
cellspacingattribute. HTMLTableElement.frameDeprecated-
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.rulesDeprecated-
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.summaryDeprecated-
A string containing a description of the purpose or the structure of the table. It reflects the obsolete
summaryattribute. HTMLTableElement.widthDeprecated-
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()-
Returns an
HTMLTableSectionElementrepresenting the first<thead>that is a child of the element. If none is found, a new one is created and inserted in the tree immediately before the first element that is neither a<caption>, nor a<colgroup>, or as the last child if there is no such element. HTMLTableElement.deleteTHead()-
Removes the first
<thead>that is a child of the element. HTMLTableElement.createTFoot()-
Returns an
HTMLTableSectionElementrepresenting the first<tfoot>that is a child of the element. If none is found, a new one is created and inserted in the tree as the last child. HTMLTableElement.deleteTFoot()-
Removes the first
<tfoot>that is a child of the element. HTMLTableElement.createTBody()-
Returns a
HTMLTableSectionElementrepresenting a new<tbody>that is a child of the element. It is inserted in the tree after the last element that is a<tbody>, or as the last child if there is no such element. HTMLTableElement.createCaption()-
Returns an
HTMLElementrepresenting the first<caption>that is a child of the element. If none is found, a new one is created and inserted in the tree as the first child of the<table>element. HTMLTableElement.deleteCaption()-
Removes the first
<caption>that is a child of the element. HTMLTableElement.insertRow()-
Returns an
HTMLTableRowElementrepresenting a new row of the table. It inserts it in the rows collection immediately before the<tr>element at the givenindexposition. If necessary a<tbody>is created. If theindexis-1, the new row is appended to the collection. If theindexis smaller than-1or greater than the number of rows in the collection, aDOMExceptionwith the valueIndexSizeErroris raised. HTMLTableElement.deleteRow()-
Removes the row corresponding to the
indexgiven in parameter. If theindexvalue is-1the last row is removed; if it is smaller than-1or greater than the amount of rows in the collection, aDOMExceptionwith the valueIndexSizeErroris raised.
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">
<tr>
<td>Row 0 Cell 0</td>
<td>Row 0 Cell 1</td>
</tr>
</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
Loading…
See also
- The HTML element implementing this interface:
<table>.