HTML DOM TFoot Object: Accessing Table Footer Elements

The HTML DOM tFoot object provides a way to access and manipulate the <tfoot> element within an HTML table using JavaScript. The <tfoot> element is used to define the footer content of a table, typically containing summary information or additional context related to the table data. Understanding how to use the tFoot object is essential for creating dynamic and interactive tables on the web. This article will guide you through the properties and methods available with the tFoot object and illustrate their usage with practical examples.

What is the HTML <tfoot> Element?

The <tfoot> element in HTML is used to define a table footer. It typically appears after the <tbody> and before any other table elements or the end of the <table> element. The <tfoot> can contain one or more <tr> (table row) elements, similar to <tbody>.

Here’s a basic structure of an HTML table with a <tfoot> element:

<table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1</td>
      <td>Data 2</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer 1</td>
      <td>Footer 2</td>
    </tr>
  </tfoot>
</table>

Purpose of the HTML DOM tFoot Object

The primary purpose of the HTML DOM tFoot object is to enable JavaScript to:

  • Access the Table Footer: Retrieve the <tfoot> element from an HTML table.
  • Modify Footer Content: Change the content of the footer dynamically.
  • Add or Remove Rows: Add new rows or remove existing rows in the footer.
  • Style the Footer: Apply styles to the footer or its contents.
  • Handle Events: Attach event listeners to the footer for interactive functionality.

Accessing the <tfoot> Element

To access the <tfoot> element using JavaScript, you first need to obtain a reference to the table element and then use its tFoot property.

<table id="myTable">
  <thead>
    <tr>
      <th>Name</th>
      <th>Age</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>30</td>
    </tr>
    <tr>
      <td>Jane Doe</td>
      <td>25</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>55</td>
    </tr>
  </tfoot>
</table>

<script>
  const table_tfoot = document.getElementById("myTable");
  const tfoot_element = table_tfoot.tFoot;

  if (tfoot_element) {
    console.log("Table footer found:", tfoot_element);
  } else {
    console.log("Table footer not found");
  }
</script>

In this example, table_tfoot.tFoot returns the <tfoot> element, which is then stored in the tfoot_element variable. If no <tfoot> element exists in the table, this will return null.

Key Properties of the tFoot Object

The tFoot object has several important properties, which are primarily related to accessing and manipulating the <tfoot> element:

` (table row) elements in the `

` element.` element (`left`, `center`, `right`, `justify`). (Deprecated in HTML5).` element. (Deprecated in HTML5)` element from the alignment character. (Deprecated in HTML5)` element (`top`, `middle`, `bottom`, `baseline`). (Deprecated in HTML5)
Property Type Description
`rows` HTMLCollection Returns a live `HTMLCollection` of all `

`align` String Sets or returns the alignment of the content within the `

`ch` String Sets or returns the alignment character for the content within the `

`chOff` String Sets or returns the offset of the content in the `

`vAlign` String Sets or returns the vertical alignment of the content within the `

Note: The align, ch, chOff, and vAlign properties are deprecated in HTML5 and should be avoided in modern web development. Use CSS to style table elements instead. ⚠️

Modifying the Footer Content

You can modify the content of the table footer dynamically using the tFoot object and the rows property.

Adding a New Row to the Footer

<table id="myTable2">
  <thead>
    <tr>
      <th>Product</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Laptop</td>
      <td>1200</td>
    </tr>
    <tr>
      <td>Mouse</td>
      <td>25</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>1225</td>
    </tr>
  </tfoot>
</table>

<script>
  const table_tfoot2 = document.getElementById("myTable2");
  const tfoot_element2 = table_tfoot2.tFoot;

  if (tfoot_element2) {
    const newRow_tfoot2 = tfoot_element2.insertRow();
    const cell1_tfoot2 = newRow_tfoot2.insertCell();
    const cell2_tfoot2 = newRow_tfoot2.insertCell();
    cell1_tfoot2.textContent = "Shipping";
    cell2_tfoot2.textContent = "10";
  }
</script>

This example uses tfoot_element2.insertRow() to add a new row to the footer and then adds cells and sets their content using insertCell() and textContent.

Removing a Row from the Footer

<table id="myTable3">
  <thead>
    <tr>
      <th>Category</th>
      <th>Items</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Electronics</td>
      <td>3</td>
    </tr>
    <tr>
      <td>Books</td>
      <td>5</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>8</td>
    </tr>
    <tr>
      <td>Discount</td>
      <td>-2</td>
    </tr>
  </tfoot>
</table>

<script>
  const table_tfoot3 = document.getElementById("myTable3");
  const tfoot_element3 = table_tfoot3.tFoot;

  if (tfoot_element3 && tfoot_element3.rows.length > 1) {
    tfoot_element3.deleteRow(1);
  }
</script>

Here, tfoot_element3.deleteRow(1) removes the second row (index 1) from the table footer. The rows property gives access to the rows in the footer.

Updating Footer Content

<table id="myTable4">
  <thead>
    <tr>
      <th>Item</th>
      <th>Quantity</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Apple</td>
      <td>10</td>
    </tr>
    <tr>
      <td>Banana</td>
      <td>15</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>25</td>
    </tr>
  </tfoot>
</table>

<script>
  const table_tfoot4 = document.getElementById("myTable4");
  const tfoot_element4 = table_tfoot4.tFoot;

  if (tfoot_element4 && tfoot_element4.rows.length > 0) {
    const firstRow_tfoot4 = tfoot_element4.rows[0];
    if(firstRow_tfoot4.cells.length > 1){
         firstRow_tfoot4.cells[1].textContent = "50";
    }
  }
</script>

In this example, we access the first row of the footer using tfoot_element4.rows[0] and then update the content of its second cell using textContent.

Styling the Footer

While the tFoot object itself doesn’t provide specific properties for styling, you can easily style the footer using CSS. Here’s an example:

<style>
  #myTable5 tfoot {
    font-weight: bold;
    background-color: #f0f0f0;
    text-align: center;
  }

  #myTable5 tfoot td {
    padding: 8px;
  }
</style>

<table id="myTable5">
  <thead>
    <tr>
      <th>City</th>
      <th>Population</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>New York</td>
      <td>8.5 million</td>
    </tr>
    <tr>
      <td>London</td>
      <td>9 million</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>17.5 million</td>
    </tr>
  </tfoot>
</table>

In this example, CSS is used to style the footer by making the text bold, applying a background color, and adding padding to the table cells. This is the modern approach for styling table elements.

Adding Event Listeners

You can add event listeners to the <tfoot> element to make it interactive.

<table id="myTable6">
  <thead>
    <tr>
      <th>Item</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Book</td>
      <td>20</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Total</td>
      <td>20</td>
    </tr>
  </tfoot>
</table>

<script>
  const table_tfoot6 = document.getElementById("myTable6");
  const tfoot_element6 = table_tfoot6.tFoot;

  if (tfoot_element6) {
    tfoot_element6.addEventListener("click", function () {
      alert("Table footer clicked!");
    });
  }
</script>

In this example, a click event listener is added to the <tfoot> element, which will display an alert when the footer is clicked.

Real-World Applications

The tFoot object is valuable in various scenarios:

  • Summary Information: Displaying totals, averages, or other summary data at the end of a table.
  • Dynamic Updates: Updating the footer content based on user interactions or data changes.
  • Enhanced Accessibility: Providing a clear footer for screen readers and other assistive technologies.
  • Interactive Tables: Adding interactive elements to the footer for filtering, sorting, or other dynamic operations.

Browser Support

The HTML DOM tFoot object is supported by all modern browsers, ensuring consistent behavior across different environments.

Note: While generally well-supported, always test your table implementations in various browsers to ensure they render and behave as expected. ✅

Conclusion

The HTML DOM tFoot object is an essential component for creating dynamic and interactive tables on the web. By understanding its properties and methods, you can effectively manipulate table footers, add interactivity, and provide clear summary information to your users. This guide has provided a comprehensive overview of the tFoot object, its usage, and its real-world applications. With these insights, you are now equipped to create more robust and user-friendly web tables.