This article explains how to dynamically insert “id” into the table element. This can be done by simply looping over the tables and add “id”s dynamically.
- The setAttribute() method adds the specified attribute to an element and gives the specified value.
table.setAttribute("id", "Dynamically Generated ID")
- It can also be done by accessing the “id” of the selected element (table).
table.id = "Dynamically Generated ID";
Output: The two tables will be created with “id”s 1 and 2 respectively.
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML course.