here2share

HTML Table Insert Demo

Feb 18th, 2018
159
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 Insert Demo</title>
  6.  
  7. </head>
  8.  
  9. <body>
  10.  
  11. <style>
  12. table {
  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>Click the button to add a new row at the first position of the table and then add cells and content.<br>
  32. <button onclick="myFunction()">Try it</button><br>
  33. </p>
  34.  
  35. <table id="myTable">
  36.   <tr>
  37.     <td>ROW:3 CELL:A</td>
  38.     <td>ROW:3 CELL:B</td>
  39.   </tr>
  40.   <tr>
  41.     <td>ROW:2 CELL:A</td>
  42.     <td>ROW:2 CELL:B</td>
  43.   </tr>
  44.   <tr>
  45.     <td>ROW:1 CELL:A</td>
  46.     <td>ROW:1 CELL:B</td>
  47.   </tr>
  48. </table>
  49. <br>
  50.  
  51. <script>
  52. var counter = 4;
  53. function myFunction() {
  54.     var table = document.getElementById("myTable");
  55.     var row = table.insertRow(0);
  56.     var cell1 = row.insertCell(0);
  57.     var cell2 = row.insertCell(1);
  58.     var string_counter = counter.toString()
  59.     cell1.innerHTML = "ROW:" + string_counter + " CELL:A (New)";
  60.     cell2.innerHTML = "ROW:" + string_counter + " CELL:B (New)";
  61.     counter++;
  62. }
  63. </script>
  64.  
  65. </body>
  66. </html>
  67.  
  68. </body>
  69.  
  70. </html>
Add Comment
Please, Sign In to add comment