HTML DOM Table Object: A Comprehensive Guide
The HTML DOM Table
object provides a powerful interface for accessing and manipulating HTML <table>
elements using JavaScript. This guide explores the various properties and methods available within the Table
object, enabling developers to dynamically interact with tables on their web pages. Whether you need to add rows, change cell content, or modify table structure, the DOM Table
object offers the necessary tools.
Understanding the HTML <table>
Element
Before diving into the DOM, it’s crucial to understand the basic structure of an HTML table:
<table>
<caption>Table Caption</caption>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data 1</td>
<td>Data 2</td>
</tr>
<tr>
<td>Data 3</td>
<td>Data 4</td>
</tr>
</tbody>
<tfoot>
<tr>
<td>Footer 1</td>
<td>Footer 2</td>
</tr>
</tfoot>
</table>
A typical table consists of:
<table>
: The root element representing the table.<caption>
: Optional caption describing the table.<thead>
: Optional header rows, usually containing<th>
(table header) elements.<tbody>
: Mandatory body rows, containing<td>
(table data) elements.<tfoot>
: Optional footer rows, often containing summary data.
Accessing Table Elements with JavaScript
To interact with a table using JavaScript, you must first obtain a reference to the <table>
element. You can achieve this using methods like getElementById
, querySelector
, or similar DOM traversal techniques.
<table id="myTable">
<!-- Table content here -->
</table>
<script>
const tableElement = document.getElementById("myTable");
// Now you can interact with the table using the 'tableElement' variable
</script>
Table Object Properties and Methods
The HTML DOM Table
object has a rich set of properties and methods, allowing you to:
Property/Method | Type | Description | |||
---|---|---|---|---|---|
`caption` | Property (Object) | Returns the table’s `
|
|||
`tHead` | Property (Object) | Returns the table’s `
` element, or `null` if none exists.
`tFoot` |
Property (Object) |
Returns the table’s `
| |
`tBodies` | Property (HTMLCollection) | Returns a collection of the table’s ` | |||
`rows` | Property (HTMLCollection) | Returns a collection of all ` | |||
`createCaption()` | Method (Object) | Creates and returns a `
|
|||
`createTHead()` | Method (Object) | Creates and returns a `
` element for the table.
`createTFoot()` |
Method (Object) |
Creates and returns a `
| |
`createTBody()` | Method (Object) | Creates and returns a ` | |||
`deleteCaption()` | Method | Deletes the table’s `
|
|||
`deleteTHead()` | Method | Deletes the table’s `
` element.
`deleteTFoot()` |
Method |
Deletes the table’s `
| |
`deleteRow(index)` | Method | Deletes a row at the specified index from the table. | |||
`insertRow(index)` | Method (Object) | Inserts and returns a new row (` | |||
Practical Examples
Let’s explore how to utilize the HTML DOM Table
object with practical examples. Each example includes the HTML and JavaScript code to demonstrate a specific function.
1. Accessing the Table Caption
This example demonstrates how to get and modify a table’s caption.
<table id="tableCaption">
<caption>Original Caption</caption>
<tbody>
<tr><td>Data</td></tr>
</tbody>
</table>
<p id="captionOutput"></p>
<script>
const tableCap = document.getElementById("tableCaption");
const captionOut = document.getElementById("captionOutput");
const tableCaption = tableCap.caption;
if (tableCaption) {
captionOut.textContent = "Caption is: " + tableCaption.textContent;
tableCaption.textContent = "New Caption";
} else {
captionOut.textContent = "No caption found";
}
</script>
Output:
Before script execution, the paragraph will be empty. After execution, if the table
had a caption, it will display: “Caption is: Original Caption” and the table caption will be updated to “New Caption.” If the table had no caption, it will display “No caption found.”
2. Creating a Table Header
This example shows how to dynamically create and add a header to a table.
<table id="tableHeader">
<tbody>
<tr><td>Data</td></tr>
</tbody>
</table>
<script>
const tableHead = document.getElementById("tableHeader");
const header = tableHead.createTHead();
const headerRow = header.insertRow();
const th1 = document.createElement("th");
const th2 = document.createElement("th");
th1.textContent = "Header 1";
th2.textContent = "Header 2";
headerRow.appendChild(th1);
headerRow.appendChild(th2);
</script>
The table will be modified to include a header:
<table id="tableHeader">
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>Data</td>
</tr>
</tbody>
</table>
3. Accessing Table Body Elements
This example demonstrates how to access and iterate through the table body elements.
<table id="tableBody">
<tbody>
<tr><td>Row 1 Data</td></tr>
<tr><td>Row 2 Data</td></tr>
</tbody>
<tbody>
<tr><td>Row 3 Data</td></tr>
<tr><td>Row 4 Data</td></tr>
</tbody>
</table>
<ul id="bodyOutput"></ul>
<script>
const tableBodyElement = document.getElementById("tableBody");
const bodyOut = document.getElementById("bodyOutput");
const bodies = tableBodyElement.tBodies;
for (let i = 0; i < bodies.length; i++) {
const body = bodies[i];
for (let j=0; j < body.rows.length; j++) {
const row = body.rows[j];
const li = document.createElement('li');
li.textContent = row.cells[0].textContent;
bodyOut.appendChild(li);
}
}
</script>
Output:
An unordered list will be created under the table with the values of table cells:
<ul id="bodyOutput">
<li>Row 1 Data</li>
<li>Row 2 Data</li>
<li>Row 3 Data</li>
<li>Row 4 Data</li>
</ul>
4. Inserting a New Row
This example illustrates how to insert a new row into a table.
<table id="tableInsertRow">
<tbody>
<tr><td>Existing Data</td></tr>
</tbody>
</table>
<script>
const tableInsert = document.getElementById("tableInsertRow");
const newRow = tableInsert.insertRow(1);
const cell = newRow.insertCell();
cell.textContent = "New Row Data";
</script>
The table structure will be modified to include the new row:
<table id="tableInsertRow">
<tbody>
<tr><td>Existing Data</td></tr>
<tr><td>New Row Data</td></tr>
</tbody>
</table>
5. Deleting a Table Row
This example demonstrates how to delete a row from a table.
<table id="tableDeleteRow">
<tbody>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
<tr><td>Row 3</td></tr>
</tbody>
</table>
<script>
const tableDel = document.getElementById("tableDeleteRow");
tableDel.deleteRow(1);
</script>
The second row will be deleted, and the table will be updated to:
<table id="tableDeleteRow">
<tbody>
<tr><td>Row 1</td></tr>
<tr><td>Row 3</td></tr>
</tbody>
</table>
6. Deleting Table Caption, Header and Footer
This example demonstrates how to delete table’s caption, header, and footer if present.
<table id="tableDeleteSection">
<caption>Existing Caption</caption>
<thead>
<tr><th>Header</th></tr>
</thead>
<tbody>
<tr><td>Data</td></tr>
</tbody>
<tfoot>
<tr><td>Footer</td></tr>
</tfoot>
</table>
<script>
const tableSec = document.getElementById("tableDeleteSection");
if (tableSec.caption) {
tableSec.deleteCaption();
}
if (tableSec.tHead) {
tableSec.deleteTHead();
}
if (tableSec.tFoot) {
tableSec.deleteTFoot();
}
</script>
After script execution, only the table body will remain in the table element.
<table id="tableDeleteSection">
<tbody>
<tr><td>Data</td></tr>
</tbody>
</table>
Browser Support
The HTML DOM Table
object and its properties and methods have excellent support across all modern browsers. You can confidently use these APIs without worrying about browser compatibility issues.
Note: Always test your code across multiple browsers to ensure a consistent experience. 💡
Conclusion
The HTML DOM Table
object offers robust capabilities for dynamically interacting with table elements in your web pages. By leveraging its properties and methods, you can create interactive tables, modify content, and control table structure efficiently. This guide provides a solid foundation for working with HTML tables using JavaScript, enabling you to build sophisticated web applications with dynamic data displays.