Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <html>
  2. <head>
  3. <style> table {width:300px; margin-bottom:20px;border-collapse:collapse; }
  4. tr,th, td {border: 1px black solid;}
  5. </style>
  6. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  7. <script>
  8.  
  9. $(document).ready(function(){
  10. $('#addform,#info,form').hide();
  11.  
  12. $('#add').click(function () {
  13. $('#addform,#info,form').fadeIn(400);
  14. });
  15.  
  16. $('#addform').click(function () {
  17. var errorFound = 0;
  18.  
  19. $('span').empty();
  20.  
  21. if($('#id').val() == '')
  22. {
  23. $('span').append("<p>ID cannot be blank!</p>");
  24. errorFound = 1;
  25. }
  26.  
  27. if($('#name').val() == '')
  28. {
  29. $('span').append("<p>Name cannot be blank!</p>");
  30. errorFound = 1;
  31. }
  32.  
  33. if($('#age').val() == '')
  34. {
  35. $('span').append("<p>Age cannot be blank!</p>");
  36. errorFound = 1;
  37. }
  38.  
  39. $('tr').each(function () {
  40. if($(this).children().first().text() == $('#id').val())
  41. {
  42. $('span').append("<p>There is already an item with this ID!</p>");
  43. errorFound = 1;
  44. }
  45. });
  46.  
  47. if(!$.isNumeric($('#id').val()))
  48. {
  49. $('span').append("<p>ID needs to be a number!</p>")
  50. errorFound = 1;
  51. }
  52.  
  53. if(!$.isNumeric($('#age').val()))
  54. {
  55. $('span').append("<p>Age needs to be a number!</p>")
  56. errorFound = 1;
  57. }
  58.  
  59. if(errorFound == 0)
  60. {
  61. $('table').append("<tr><td>" + $("#id").val() +"</td><td>" + $('#name').val() +"</td><td>" + $('#age').val() + "</td></tr>");
  62. }
  63. });
  64.  
  65. $('table').on('mouseenter', 'tr', function () {
  66. $(this).css('color', "red");
  67. });
  68.  
  69. $('table').on('mouseleave', 'tr', function() {
  70. $(this).css('color', "");
  71. });
  72.  
  73. $("tr").dblclick(function() {
  74. $(this).remove();
  75. });
  76. });
  77. </script>
  78. </head>
  79. <body>
  80. <table>
  81. <tr>
  82. <th>ID</th>
  83. <th>Name</th>
  84. <th>Age</th>
  85. </tr>
  86. <tr>
  87. <td>1</td>
  88. <td>Messi</td>
  89. <td>28</td>
  90. </tr>
  91. <tr>
  92. <td>2</td>
  93. <td>Ronaldo</td>
  94. <td>30</td>
  95. </tr>
  96.  
  97. </table>
  98. <div id ="info">Please enter information:</div>
  99. <form>
  100. <span style="color:red"> </span>
  101. <p>ID: <input id ='id' type = 'text'/> </p>
  102. <p>Name: <input id = 'name' type = 'text'/></p>
  103. <p>Age: <input id ='age' type = 'text'/></p>
  104. </form>
  105. <button id = "add" >Add</button>
  106. <button id = "addform">Add to Table</button></body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement