HTMLTableElement
Table Element
Represents a <table> tag. Has dedicated methods to add/remove rows and cells without building HTML strings.
<table></table>
Key Properties
| Property | Type | Description |
|---|---|---|
| rows | HTMLCollection | All <tr> rows in the table. Live-updating collection. |
| caption | element | The <caption> element for the table, or null. |
Common Methods
| Method | Returns | Description |
|---|---|---|
| insertRow() | HTMLTableRowElement | Adds a new <tr> and returns it. Pass index or -1 for end. |
| deleteRow() | void | Removes a row by index. |
Quick Examples
JS
const table = document.querySelector('table');
// Build a table from data
const data = [
['Alice', 'Engineer', 'London'],
['Bob', 'Designer', 'Berlin'],
];
data.forEach(([name, role, city]) => {
const row = table.insertRow(-1);
row.insertCell(0).textContent = name;
row.insertCell(1).textContent = role;
row.insertCell(2).textContent = city;
});
// Delete a row when clicked
table.addEventListener('click', (e) => {
const row = e.target.closest('tr');
if (row) row.remove();
});Related Elements
Related Properties
Related Methods