Table manipulation
Table manipulation means using JavaScript to add, read, update, sort and delete rows and cells in an HTML <table> while the page is open. It is how dashboards show live data, how shopping carts update quantities, and how admin screens let you remove records without reloading. Tables have their own dedicated DOM properties and methods that make this easier than building rows by hand.
Table-specific DOM properties
Besides the usual DOM methods, table elements expose shortcuts:
table.tHead,table.tBodies,table.tFoot: the table's sections.table.rowsandtbody.rows: live collections of<tr>elements.row.cells: the<td>and<th>cells in a row.row.rowIndex(position in the whole table) androw.sectionRowIndex(position within its section).insertRow(index),deleteRow(index),row.insertCell(index): create and remove rows and cells. An index of-1, or leaving it out, means "at the end".
Building rows from data and deleting them
<table id="students">
<thead>
<tr><th>Name</th><th>Marks</th><th></th></tr>
</thead>
<tbody></tbody>
</table>
const data = [
{ id: 1, name: 'Anya', marks: 88 },
{ id: 2, name: 'Rohit', marks: 72 },
{ id: 3, name: 'Zara', marks: 95 },
];
const tbody = document.querySelector('#students').tBodies[0];
function render(rows) {
tbody.replaceChildren(); // clear old rows
for (const s of rows) {
const tr = tbody.insertRow();
tr.dataset.id = s.id;
tr.insertCell().textContent = s.name;
tr.insertCell().textContent = s.marks;
const btn = document.createElement('button');
btn.textContent = 'Delete';
tr.insertCell().append(btn);
}
}
// One listener handles every delete button
tbody.addEventListener('click', (e) => {
if (e.target.tagName !== 'BUTTON') return;
const tr = e.target.closest('tr');
const index = data.findIndex((s) => s.id === Number(tr.dataset.id));
data.splice(index, 1);
tr.remove();
});
render(data);
// Sort by marks, highest first, then redraw
document.querySelector('#students thead').addEventListener('click', () => {
render([...data].sort((a, b) => b.marks - a.marks));
});
Why insert rows into the tbody
Calling insertRow() directly on the table works, but it decides for you where the row goes, and with a header present the result can be surprising. Targeting table.tBodies[0] makes it explicit that data rows belong in the body and keeps the <thead> untouched, which matters when you clear and re-render the body.
The example keeps the data in an array and redraws from it. This is easier to reason about than reading values back out of cells, especially for sorting. When sorting by text, use a.name.localeCompare(b.name); when sorting numbers read from cells, convert them with Number() first, or "100" will sort before "9".
The innerHTML security hole
A tempting shortcut is building rows as strings: tbody.innerHTML += `<tr><td>${name}</td></tr>`. If name comes from a user or an API and contains HTML such as an <img onerror> tag, the browser runs it. This is called cross-site scripting. Setting textContent on each cell, as above, treats the value as plain text and is safe. Using += with innerHTML also rebuilds the whole body each time, throwing away any listeners attached directly to old rows.
Event delegation, with one listener on the tbody, avoids that problem and keeps working for rows added later.
Updating a single cell
You do not always need a full redraw. To change one value, find the row and then the cell by position: tbody.rows[1].cells[1].textContent = 80; updates Rohit's marks. Because rows and cells are live collections, they always reflect the current table, so indexes shift after a row is removed. Looking rows up by a stable data-id, for example with tbody.querySelector('tr[data-id="2"]'), is more reliable than remembering positions. Keep the array and the table in step by updating both whenever a value changes.
Takeaway
Work on tBodies[0], create rows with insertRow and insertCell, fill them with textContent, handle row actions with a single delegated listener, and keep your data in an array so sorting and filtering are simple redraws.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.