| title | HTMLTableElement |
|---|---|
| slug | Web/API/HTMLTableElement |
| page-type | web-api-interface |
| browser-compat | api.HTMLTableElement |
{{APIRef("HTML DOM")}}
The HTMLTableElement interface provides special properties and methods (beyond the regular {{DOMxRef("HTMLElement")}} object interface it also has available to it by inheritance) for manipulating the layout and presentation of tables in an HTML document.
{{InheritanceDiagram}}
Inherits properties from its parent, {{DOMxRef("HTMLElement")}}.
- {{DOMxRef("HTMLTableElement.caption")}}
- : A {{DOMxRef("HTMLTableCaptionElement")}} representing the first {{HTMLElement("caption")}} that is a child of the element, or
nullif none is found. When set, if the object doesn't represent a<caption>, a {{DOMxRef("DOMException")}} with 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.
- : A {{DOMxRef("HTMLTableCaptionElement")}} representing the first {{HTMLElement("caption")}} that is a child of the element, or
- {{DOMxRef("HTMLTableElement.tHead")}}
- : A {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("thead")}} that is a child of the element, or
nullif none is found. When set, if the object doesn't represent a<thead>, a {{DOMxRef("DOMException")}} with theHierarchyRequestErrorname is thrown. If a correct object is given, it is inserted in the tree immediately before the first element that is neither a {{HTMLElement("caption")}}, nor a {{HTMLElement("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.
- : A {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("thead")}} that is a child of the element, or
- {{DOMxRef("HTMLTableElement.tFoot")}}
- : A {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("tfoot")}} that is a child of the element, or
nullif none is found. When set, if the object doesn't represent a<tfoot>, a {{DOMxRef("DOMException")}} with theHierarchyRequestErrorname is thrown. If a correct object is given, it is inserted in the tree immediately before the first element that is neither a {{HTMLElement("caption")}}, a {{HTMLElement("colgroup")}}, nor a {{HTMLElement("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.
- : A {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("tfoot")}} that is a child of the element, or
- {{DOMxRef("HTMLTableElement.rows")}} {{ReadOnlyInline}}
- : Returns a live {{DOMxRef("HTMLCollection")}} containing all the rows of the element, that is all {{HTMLElement("tr")}} that are a child of the element, or a child of one of its {{HTMLElement("thead")}}, {{HTMLElement("tbody")}} and {{HTMLElement("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.
- : Returns a live {{DOMxRef("HTMLCollection")}} containing all the rows of the element, that is all {{HTMLElement("tr")}} that are a child of the element, or a child of one of its {{HTMLElement("thead")}}, {{HTMLElement("tbody")}} and {{HTMLElement("tfoot")}} children. The rows members of a
- {{DOMxRef("HTMLTableElement.tBodies")}} {{ReadOnlyInline}}
- : Returns a live {{DOMxRef("HTMLCollection")}} containing all the {{HTMLElement("tbody")}} of the element. The
HTMLCollectionis live and is automatically updated when theHTMLTableElementchanges.
- : Returns a live {{DOMxRef("HTMLCollection")}} containing all the {{HTMLElement("tbody")}} of the element. The
Warning
The following properties are obsolete. You should avoid using them.
- {{DOMxRef("HTMLTableElement.align")}} {{deprecated_inline}}
- : 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".
- : A string containing an enumerated value reflecting the
- {{DOMxRef("HTMLTableElement.bgColor")}} {{deprecated_inline}}
- : A string containing the background color of the cells. It reflects the obsolete
bgColorattribute.
- : A string containing the background color of the cells. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.border")}} {{deprecated_inline}}
- : A string containing the width in pixels of the border of the table. It reflects the obsolete
borderattribute.
- : A string containing the width in pixels of the border of the table. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.cellPadding")}} {{deprecated_inline}}
- : A string containing the width in pixels of the horizontal and vertical space between cell content and cell borders. It reflects the obsolete
cellpaddingattribute.
- : A string containing the width in pixels of the horizontal and vertical space between cell content and cell borders. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.cellSpacing")}} {{deprecated_inline}}
- : A string containing the width in pixels of the horizontal and vertical separation between cells. It reflects the obsolete
cellspacingattribute.
- : A string containing the width in pixels of the horizontal and vertical separation between cells. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.frame")}} {{deprecated_inline}}
- : 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".
- : A string containing the type of the external borders of the table. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.rules")}} {{deprecated_inline}}
- : 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".
- : A string containing the type of the internal borders of the table. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.summary")}} {{deprecated_inline}}
- : A string containing a description of the purpose or the structure of the table. It reflects the obsolete
summaryattribute.
- : A string containing a description of the purpose or the structure of the table. It reflects the obsolete
- {{DOMxRef("HTMLTableElement.width")}} {{deprecated_inline}}
- : A string containing the length in pixels or in percentage of the desired width of the entire table. It reflects the obsolete
widthattribute.
- : A string containing the length in pixels or in percentage of the desired width of the entire table. It reflects the obsolete
Inherits methods from its parent, {{DOMxRef("HTMLElement")}}.
- {{DOMxRef("HTMLTableElement.createTHead()")}}
- : Returns an {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("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 {{HTMLElement("caption")}}, nor a {{HTMLElement("colgroup")}}, or as the last child if there is no such element.
- {{DOMxRef("HTMLTableElement.deleteTHead()")}}
- : Removes the first {{HTMLElement("thead")}} that is a child of the element.
- {{DOMxRef("HTMLTableElement.createTFoot()")}}
- : Returns an {{DOMxRef("HTMLTableSectionElement")}} representing the first {{HTMLElement("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.
- {{DOMxRef("HTMLTableElement.deleteTFoot()")}}
- : Removes the first {{HTMLElement("tfoot")}} that is a child of the element.
- {{DOMxRef("HTMLTableElement.createTBody()")}}
- : Returns a {{DOMxRef("HTMLTableSectionElement")}} representing a new {{HTMLElement("tbody")}} that is a child of the element. It is inserted in the tree after the last element that is a {{HTMLElement("tbody")}}, or as the last child if there is no such element.
- {{DOMxRef("HTMLTableElement.createCaption()")}}
- : Returns an {{DOMxRef("HTMLElement")}} representing the first {{HTMLElement("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 {{HTMLElement("table")}} element.
- {{DOMxRef("HTMLTableElement.deleteCaption()")}}
- : Removes the first {{HTMLElement("caption")}} that is a child of the element.
- {{DOMxRef("HTMLTableElement.insertRow()")}}
- : Returns an {{DOMxRef("HTMLTableRowElement")}} representing a new row of the table. It inserts it in the rows collection immediately before the {{HTMLElement("tr")}} element at the given
indexposition. If necessary a {{HTMLElement("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, a {{DOMxRef("DOMException")}} with the valueIndexSizeErroris raised.
- : Returns an {{DOMxRef("HTMLTableRowElement")}} representing a new row of the table. It inserts it in the rows collection immediately before the {{HTMLElement("tr")}} element at the given
- {{DOMxRef("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, a {{DOMxRef("DOMException")}} with the valueIndexSizeErroris raised.
- : Removes the row corresponding to the
The HTMLTableElement interface provides some convenience methods for creating and manipulating tables. Two frequently used methods are {{domxref("HTMLTableElement.insertRow")}} and {{domxref("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));
}{{EmbedLiveSample("using_the_dom_table_interface", "", "300")}}
{{Specifications}}
{{Compat}}
- The HTML element implementing this interface: {{HTMLElement("table")}}.