Advertisement
here2share

HTML Table Array Loop Demo

Feb 18th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2. <html>
  3.  
  4. <head>
  5.     <title>HTML Table Array Loop Demo</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10.  
  11. <style>
  12. table, p {
  13.     font-family: arial, sans-serif;
  14.     border-collapse: collapse;
  15.     width: 100%;
  16. }
  17.  
  18. td, th {
  19.     border: 1px solid #dddddd;
  20.     text-align: left;
  21.     padding: 8px;
  22. }
  23.  
  24. tr:nth-child(even) {
  25.     background-color: #dddddd;
  26. }
  27. </style>
  28.  
  29. <body>
  30.  
  31. <p>PETS</p>
  32. <table id="myTable">
  33. </table>
  34. <br>
  35.  
  36. <script>
  37. function myFunction() {
  38.     var pets = ["Turtle", "Dog", "Cat", "Bird", "Fish"];
  39.     var i = 0;
  40.     while (i < pets.length) {
  41.         var table = document.getElementById("myTable");
  42.         var row = table.insertRow();
  43.         var cell1 = row.insertCell(0);
  44.         var cell2 = row.insertCell(1);
  45.         cell1.innerHTML = pets[i];
  46.         cell2.innerHTML = i+1;
  47.         i++;
  48.     }
  49. }
  50. myFunction()
  51. </script>
  52.  
  53. </body>
  54. </html>
  55.  
  56. </body>
  57.  
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement