HTML TableRow rowIndex Property: Table Row Index

June 19, 2025

HTML TableRow rowIndex Property: Getting the Table Row Index

The rowIndex property in HTML DOM is used to retrieve the index of a <tr> (table row) element within a table. This property is extremely useful when you need to programmatically access or manipulate specific rows in a table using JavaScript. It provides a straightforward way to identify the position of a row.

Definition and Purpose

The rowIndex property returns the index of a row in the <table>. The index starts at 0 for the first row, 1 for the second row, and so on. This property is read-only.

Syntax

rowObject.rowIndex;

Here, rowObject is a reference to a <tr> element.

Return Value

  • An Integer, representing the row’s index within the table.

Practical Examples

Let’s dive into some practical examples to illustrate how to use the rowIndex property.

Basic Example: Displaying Row Index

In this example, we’ll create a simple table and use JavaScript to display the index of a clicked row.

<!DOCTYPE html>
<html>
<head>
    <title>TableRow rowIndex Example</title>
</head>
<body>
    <table id="myTable_row_index" border="1">
        <tr><td>Row 1</td></tr>
        <tr><td>Row 2</td></tr>
        <tr><td>Row 3</td></tr>
    </table>
    <p>Click a row to display its index.</p>
    <p id="rowIndexDisplay_row_index"></p>

    <script>
        const table_row_index = document.getElementById("myTable_row_index");
        const display_row_index = document.getElementById("rowIndexDisplay_row_index");

        table_row_index.addEventListener("click", function(event) {
            const row_row_index = event.target.parentNode;
            const index_row_index = row_row_index.rowIndex;
            display_row_index.textContent = "Row index: " + index_row_index;
        });
    </script>
</body>
</html>

Output:

Clicking on a row in the table will display its index below the table. For instance, clicking on “Row 2” will display “Row index: 1”.

Example 2: Highlighting a Row Based on Index

In this example, we’ll highlight a row based on its index.

<!DOCTYPE html>
<html>
<head>
    <title>TableRow rowIndex Highlighting Example</title>
    <style>
        .highlight_row_index {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <table id="highlightTable_row_index" border="1">
        <tr><td>Row 1</td></tr>
        <tr><td>Row 2</td></tr>
        <tr><td>Row 3</td></tr>
    </table>
    <button id="highlightButton_row_index">Highlight Row 2</button>

    <script>
        const highlightTable_row_index = document.getElementById("highlightTable_row_index");
        const highlightButton_row_index = document.getElementById("highlightButton_row_index");

        highlightButton_row_index.addEventListener("click", function() {
            const rows_row_index = highlightTable_row_index.rows;
            // Note: rowIndex is relative to the table, not the rows collection.
            // The rows collection only contains the <tr> elements, but the table's rowIndex
            // accounts for all rows, including those in <thead>, <tbody>, and <tfoot>.
            if (rows_row_index.length > 1) { // Ensure the table has at least two rows
               rows_row_index[1].classList.add("highlight_row_index"); // Highlight the second row (index 1)
            }

        });
    </script>
</body>
</html>

Output:

Clicking the “Highlight Row 2” button will change the background color of the second row to yellow.

Example 3: Deleting a Row Using rowIndex

This example demonstrates how to delete a row from a table using its index.

<!DOCTYPE html>
<html>
<head>
    <title>TableRow rowIndex Deletion Example</title>
</head>
<body>
    <table id="deleteTable_row_index" border="1">
        <tr><td>Row 1</td></tr>
        <tr><td>Row 2</td></tr>
        <tr><td>Row 3</td></tr>
    </table>
    <button id="deleteButton_row_index">Delete Row 2</button>

    <script>
        const deleteTable_row_index = document.getElementById("deleteTable_row_index");
        const deleteButton_row_index = document.getElementById("deleteButton_row_index");

        deleteButton_row_index.addEventListener("click", function() {
            const rowIndexToDelete_row_index = 1; // Index of the row to delete
            deleteTable_row_index.deleteRow(rowIndexToDelete_row_index);
        });
    </script>
</body>
</html>

Output:

Clicking the “Delete Row 2” button will remove the second row from the table.

Example 4: Dynamically Displaying All Row Indices

In this example, we’ll dynamically display the index of each row in a table next to its content.

<!DOCTYPE html>
<html>
<head>
    <title>TableRow Dynamic rowIndex Display Example</title>
</head>
<body>
    <table id="dynamicTable_row_index" border="1">
        <tr><td>Row 1</td></tr>
        <tr><td>Row 2</td></tr>
        <tr><td>Row 3</td></tr>
    </table>

    <script>
        const dynamicTable_row_index = document.getElementById("dynamicTable_row_index");
        const rowsDynamic_row_index = dynamicTable_row_index.rows;

        for (let i = 0; i < rowsDynamic_row_index.length; i++) {
            const rowIndexDynamic_row_index = rowsDynamic_row_index[i].rowIndex;
            rowsDynamic_row_index[i].cells[0].textContent += " (Index: " + rowIndexDynamic_row_index + ")";
        }
    </script>
</body>
</html>

Output:

Each row in the table will display its index next to its content. For example, “Row 1 (Index: 0)”, “Row 2 (Index: 1)”, and “Row 3 (Index: 2)”.

Key Considerations

  • rowIndex vs. Collection Index: The rowIndex property reflects the row’s position within the entire table, including rows in the <thead>, <tbody>, and <tfoot> sections. The index of a row in the rows collection (e.g., table.rows[i]) is relative only to that collection.
  • Read-Only: The rowIndex property is read-only, meaning you cannot set it to change the row’s position.
  • Dynamic Tables: When rows are added or removed, the rowIndex values of subsequent rows are automatically updated.

Use Cases

  • Dynamic Table Manipulation: Accessing and modifying specific rows based on user interaction.
  • Data Processing: Performing operations on rows based on their position in the table.
  • UI Enhancements: Highlighting or styling rows based on their index.

Conclusion

The rowIndex property is a valuable tool for working with tables in HTML DOM. It simplifies the process of identifying and manipulating rows, enabling dynamic and interactive web applications. Understanding its usage and considerations will help you efficiently manage table data in your projects.