Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <style>
  6. *, html, body {
  7. margin: 0px;
  8. padding: 0px;
  9. }
  10. table {
  11. border-collapse: collapse;
  12. }
  13. table td {
  14. border : 1px solid black;
  15. }
  16. </style>
  17. <script>
  18. document.addEventListener('DOMContentLoaded', function () {
  19. var addButton =
  20. document.getElementById('add');
  21. var removeButton =
  22. document.getElementById('remove');
  23. var tableTbody = document.querySelector('table > tbody');
  24.  
  25. var columnsCount = ((tableTbody.querySelector('tr') === null)
  26. || (tableTbody.querySelector('tr > td') === null))
  27. ? 3
  28. : tableTbody.querySelector('tr').childElementCount;
  29.  
  30. function generateRandomString(len) {
  31. function randomInt(min, max) {
  32. return Math.floor(Math.random() * (max - min + 1)) + min;
  33. }
  34.  
  35. var alphabet = 'qwertyuiopasdfghjklzxcvbnm';
  36. alphabet += alphabet.toUpperCase();
  37. alphabet += '1234567890';
  38. var result = '';
  39. for (var i=0; i<len; i++) {
  40. result += alphabet[randomInt(0, alphabet.length)];
  41. }
  42. return result;
  43. }
  44.  
  45. addButton.addEventListener('click', function () {
  46. var html = '<tr>';
  47. for (var i=0; i<columnsCount; i++) {
  48. html += '<td>';
  49. html += generateRandomString(10);
  50. html += '</td>';
  51. }
  52. html += '</tr>';
  53. tableTbody.innerHTML += html;
  54. });
  55.  
  56. removeButton.addEventListener('click', function () {
  57. tableTbody.children[tableTbody.childElementCount-1].remove
  58. ? tableTbody.children[tableTbody.childElementCount-1].remove()
  59. // Small IE11 fix:
  60. : tableTbody.children[tableTbody.childElementCount-1].removeNode();
  61. });
  62.  
  63. });
  64. </script>
  65. </head>
  66. <body>
  67. <button id="add">Add</button>
  68. <button id="remove">Remove</button>
  69. <table> <!-- Script support any table with tr tag or without it -->
  70. <tbody>
  71. <tr>
  72. <td>
  73. asd
  74. </td>
  75. <td>
  76. dsa
  77. </td>
  78. <td>
  79. qwe
  80. </td>
  81. <td>
  82. ewq
  83. </td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. </body>
  88. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement