简体   繁体   中英

How to set dynamic id to a table and append it to a div?

I want to dynamically create 10 tables and append it in div container with class table_container. This is the snippet I was trying, do i need to make any changes in it for my requirement?

 for( let i =0; i< 10; i++){ var table = document.createElement('table'); table.setAttribute("id", "table"+i); }
 <div class="table_container"> </div>

// Get a reference to the container.
const container = document.querySelectorAll('.table_container')[0];

for(let i = 0; i < 10; i++){
  // Create the new <table>
  const table = document.createElement('table');
  table.setAttribute("id", "table"+i);
  
  // Append it to the inside of the container.
  container.appendChild(table);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM