aakash2310

Untitled

Jul 28th, 2024
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Dynamic HTML Table</title>
  5. <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  6. <script>
  7. $(document).ready(function() {
  8. let currentRow = null;
  9. $('#insertRowBtn').click(function() {
  10. var newRow = '<tr>' +
  11. '<td><input type="text" class="rollNo"></td>' +
  12. '<td><input type="text" class="name"></td>' +
  13. '<td><input type="text" class="gender"></td>' +
  14. '<td><input type="text" class="age"></td>' +
  15. '</tr>';
  16. $('table tbody').append(newRow);
  17. });
  18. $(document).on('focus', 'input', function() {
  19. currentRow = $(this).closest('tr');
  20. });
  21. $('#deleteRowBtn').click(function() {
  22. if (currentRow) {
  23. currentRow.remove();
  24. }
  25. });
  26. });
  27. </script>
  28. </head>
  29. <body>
  30. <table>
  31. <thead>
  32. <tr>
  33. <th>RollNo.</th>
  34. <th>Name</th>
  35. <th>Gender</th>
  36. <th>Age</th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <tr>
  41. <td><input type="text" class="rollNo"></td>
  42. <td><input type="text" class="name"></td>
  43. <td><input type="text" class="gender"></td>
  44. <td><input type="text" class="age"></td>
  45. </tr>
  46. <tr>
  47. <td><input type="text" class="rollNo"></td>
  48. <td><input type="text" class="name"></td>
  49. <td><input type="text" class="gender"></td>
  50. <td><input type="text" class="age"></td>
  51. </tr>
  52. </tbody>
  53. </table>
  54. <button id="insertRowBtn">Insert Row</button>
  55. <button id="deleteRowBtn">Delete Current Row</button>
  56. </body>
  57. </html>
Advertisement
Add Comment
Please, Sign In to add comment