How to dynamically insert id into a table element using JavaScript?


In this article, we will learn how to dynamically insert IDs into a table element using JavaScrip with the help of setAttribute API.

In web development, dynamically inserting an ID into an HTML table element can be a useful technique when we need to uniquely identify and manipulate specific rows or cells. By assigning IDs to table elements programmatically, we gain more control and flexibility in accessing and modifying the table's content.

Let’s understand how to implement this with the help of some examples.

Example 1

In this example, we generate a random id for a table row by computing the total number of rows available and appending that count to an empty string.

Filename: index.html

<html lang="en">
   <head>
      <title>How to dynamically insert id into table element using JavaScript?</title>
   </head>
   <body>
      <h3>How to dynamically insert id into table element using JavaScript?</h3>
      <table id="myTable">
         <tr>
            <th>Row</th>
            <th>Data</th>
         </tr>
      </table>
      <button onclick="addRow()">Add Row</button>
      <script>
         function addRow() {
            const table = document.getElementById("myTable");
            const row = table.insertRow();
            const rowId = "" + (table.rows.length - 1); // Generate a unique ID
            row.id = rowId; // Set the ID of the row

            // Add cells to the new row
            const cell1 = row.insertCell(0);
            const cell2 = row.insertCell(1);

            // Insert content into cells
            cell1.innerHTML = "Row " + (table.rows.length - 1);
            cell2.innerHTML = "Data " + (table.rows.length - 1);
         }
      </script>
   </body>
</html>

Example 2

In this example, we will follow the above code pattern, and we will generate a random id for a table cell using 3 different methods, with the help of classList method, classList object, and id property of the dataset object.

Filename: index.html

<html lang="en">
   <head>
      <title>
         How to dynamically insert id into table element using JavaScript?
      </title>
   </head>
   <body>
      <h3>How to dynamically insert id into table element using JavaScript?</h3>
      <table id="myTable">
         <tr>
            <th>Row</th>
            <th>Data</th>
         </tr>
      </table>
      <button onclick="addRowUsingObj()">Add Row Using classList object</button>
      <button onclick="addRowUsingMethod()">
         Add Row Using classList method
      </button>
      <button onclick="addRowUsingId()">Add Row Using the id property</button>
      <script>
         const table = document.getElementById("myTable");

         function addRowUsingObj() {
            // Example 1: Using classList object
            const row1 = table.insertRow();
            row1.classList.add("custom-row");

            // Insert content into cells
            const cell1 = row1.insertCell();
            const cell2 = row1.insertCell();
            cell1.innerHTML = "Row " + (table.rows.length - 1);
            cell2.innerHTML = "Data " + (table.rows.length - 1);
         }
 
         function addRowUsingMethod() {
            // Example 2: Using classList method
            const row3 = table.insertRow();
            row3.classList.add("row-highlight");

            // Insert content into cells
            const cell1 = row3.insertCell();
            const cell2 = row3.insertCell();
            cell1.innerHTML = "Row " + (table.rows.length - 1);
            cell2.innerHTML = "Data " + (table.rows.length - 1);
         }

         function addRowUsingId() {
            // Example 3: Using the id property of the dataset object
            const row4 = table.insertRow();
            const rowId = "row-" + (table.rows.length - 1); // Generate a unique ID
            row4.dataset.id = rowId; // Set the ID of the row

            // Insert content into cells
            const cell1 = row4.insertCell();
            const cell2 = row4.insertCell();
            cell1.innerHTML = "Row " + (table.rows.length - 1);
            cell2.innerHTML = "Data " + (table.rows.length - 1);
            // Add more cells and content for the other examples if needed
         }
      </script>
   </body>
</html>

Conclusion

In conclusion, dynamically inserting IDs into table elements using JavaScript allows for efficient manipulation and interaction with specific elements within a table. In the provided examples, we learned how to dynamically insert IDs into table rows and cells. By utilizing JavaScript methods such as insertRow, insertCell, and manipulating the id property, we generated unique IDs and associated them with the corresponding table elements.

Updated on: 03-Aug-2023

597 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements