files/en-us/web/api/htmltableelement/index.md
{{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")}}.
null if none is found. When set, if the object doesn't represent a <caption>, a {{DOMxRef("DOMException")}} with the HierarchyRequestError name 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.null if none is found. When set, if the object doesn't represent a <thead>, a {{DOMxRef("DOMException")}} with the HierarchyRequestError name 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.null if none is found. When set, if the object doesn't represent a <tfoot>, a {{DOMxRef("DOMException")}} with the HierarchyRequestError name 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.<thead> appear first, in tree order, and those members of a <tbody> last, also in tree order. The HTMLCollection is live and is automatically updated when the HTMLTableElement changes.HTMLCollection is live and is automatically updated when the HTMLTableElement changes.[!WARNING] The following properties are obsolete. You should avoid using them.
align attribute. It indicates the alignment of the element's contents with respect to the surrounding context. The possible values are "left", "right", and "center".bgColor attribute.border attribute.cellpadding attribute.cellspacing attribute.frame attribute and can take one of the following values: "void", "above", "below", "hsides", "vsides", "lhs", "rhs", "box", or "border".rules attribute and can take one of the following values: "none", "groups", "rows", "cols", or "all".summary attribute.width attribute.Inherits methods from its parent, {{DOMxRef("HTMLElement")}}.
index position. If necessary a {{HTMLElement("tbody")}} is created. If the index is -1, the new row is appended to the collection. If the index is smaller than -1 or greater than the number of rows in the collection, a {{DOMxRef("DOMException")}} with the value IndexSizeError is raised.index given in parameter. If the index value is -1 the last row is removed; if it is smaller than -1 or greater than the amount of rows in the collection, a {{DOMxRef("DOMException")}} with the value IndexSizeError is raised.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}}