aakash2310

Untitled

Aug 8th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 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.  
  9. let currentRow = null;
  10.  
  11. $('#insertRowBtn').click(function() {
  12. var newRow = '<tr>' +
  13. '<td><input type="text" class="rollNo"></td>' +
  14. '<td><input type="text" class="name"></td>' +
  15. '<td><input type="text" class="gender"></td>' +
  16. '<td><input type="text" class="age"></td>' +
  17. '</tr>';
  18. $('table tbody').append(newRow);
  19. });
  20.  
  21. $(document).on('focus', 'input', function() {
  22. currentRow = $(this).closest('tr');
  23. });
  24.  
  25. $('#deleteRowBtn').click(function() {
  26. if (currentRow) {
  27. currentRow.remove();
  28. }
  29. });
  30.  
  31. $(document).on('input', '.gender', function() {
  32. var value = $(this).val();
  33. if (value !== 'F' && value !== 'M' && value !== 'O') {
  34. alert('Invalid gender value. Valid values are F, M, O.');
  35. $(this).val('');
  36. }
  37. else
  38. {
  39. if (value === 'F') {
  40. $(this).closest('tr').css('background-color', 'green');
  41. }
  42. }
  43. });
  44.  
  45. $(document).on('input', '.age', function() {
  46. var value = $(this).val();
  47. if (value <= 0 || isNaN(value)) {
  48. alert('Invalid age value. Age must be a positive number.');
  49. $(this).val('');
  50. } else {
  51. if (value < 18) {
  52. $(this).closest('tr').find('.age').css('font-weight', 'bold');
  53. } else {
  54. $(this).closest('tr').find('.age').css('font-weight', 'normal');
  55. }
  56. }
  57. });
  58. });
  59. </script>
  60. </head>
  61. <body>
  62. <table>
  63. <thead>
  64. <tr>
  65. <th>RollNo.</th>
  66. <th>Name</th>
  67. <th>Gender</th>
  68. <th>Age</th>
  69. </tr>
  70. </thead>
  71. <tbody>
  72. <tr>
  73. <td><input type="text" class="rollNo"></td>
  74. <td><input type="text" class="name"></td>
  75. <td><input type="text" class="gender"></td>
  76. <td><input type="text" class="age"></td>
  77. </tr>
  78. <tr>
  79. <td><input type="text" class="rollNo"></td>
  80. <td><input type="text" class="name"></td>
  81. <td><input type="text" class="gender"></td>
  82. <td><input type="text" class="age"></td>
  83. </tr>
  84. </tbody>
  85. </table>
  86. <button id="insertRowBtn">Insert Row</button>
  87. <button id="deleteRowBtn">Delete Current Row</button>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment