Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Failed to looping javascript with HTML table

I failed to looping data with javascript and show it inside HTML table, the failed is the data that shown is not right as i expected, the data is outside the table, how to make the data show inside the table

HTML code :

  <table style="border: 1px solid">
    <thead>
      <th>Name</th>
    </thead>
    <tbody>
      <div id="demo"></div>
    </tbody>
  </table>

Javascript code :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      const cars = ["BMW", "Volvo", "Saab", "Ford"];

      let i = 0;
      let text = "";

      for (let i = 0; i < cars.length; i++) {
        text += "<tr> <td>"+ cars[i] + "</td> </tr>";
      }
      document.getElementById("demo").innerHTML = text;

The result :
enter image description here

>Solution :

Just do following changes :

 <table style="border: 1px solid">
    <thead>
      <th>Name</th>
    </thead>
    <tbody id="demo"></tbody>
  </table>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading