Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. <body>
  2.  
  3. <div id="page_container">
  4.  
  5. <div class="form_container">
  6.  
  7. <h3>Add and Delete rows dynamically with textboxes using jQuery:</h3>
  8.  
  9. <table id="expense_table" cellspacing="0" cellpadding="0">
  10. <thead>
  11. <tr>
  12. <th>Sl. No</th>
  13. <th>Month</th>
  14.  
  15.  
  16. <th>&nbsp;</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <td><input type="text" name="reg_no_01" maxlength="10" required /></td>
  22. <td><input type="text" name="subjects_01" maxlength="10" required /></td>
  23.  
  24. <td>&nbsp;</td>
  25. </tr>
  26. </tbody>
  27. </table>
  28.  
  29. <input type="button" value="Add Row" id="add_ExpenseRow" />
  30.  
  31. </div> <!-- END subject_marks -->
  32. <div class="clearfix"></div>
  33.  
  34. </div>
  35.  
  36. <!-- FOOTER -->
  37. <a class="mm" href="http://mediamilan.com/" title="Go to Media Milan.com" target="_blank"></a>
  38. <footer> <p>Developed by : Miteshan Patel</p> </footer>
  39.  
  40. </body>
  41.  
  42. jquery
  43.  
  44. $(function(){
  45. // GET ID OF last row and increment it by one
  46. var $lastChar =1, $newRow;
  47. $get_lastID = function(){
  48. var $id = $('#expense_table tr:last-child td:first-child input').attr("name");
  49. $lastChar = parseInt($id.substr($id.length - 2), 10);
  50. console.log('GET id: ' + $lastChar + ' | $id :'+$id);
  51. $lastChar = $lastChar + 1;
  52. $newRow = "<tr>
  53. <td><input type='text' name='reg_no_0"+$lastChar+"' maxlength='10' /></td>
  54. <td><input type='text' name='subjects_0"+$lastChar+"' maxlength='10' /></td>
  55. <td><input type='button' value='Delete' class='del_ExpenseRow' /></td>
  56. </tr>"
  57. return $newRow;
  58. }
  59.  
  60. // ***** -- START ADDING NEW ROWS
  61. $('#add_ExpenseRow').on("click", function(){
  62. if($lastChar <= 9){
  63. $get_lastID();
  64. $('#expense_table tbody').append($newRow);
  65. } else {
  66. alert("Reached Maximum Rows!");
  67. };
  68. });
  69.  
  70. $(".del_ExpenseRow").on("click", function(){
  71. $(this).closest('tr').remove();
  72. $lastChar = $lastChar-2;
  73. });
  74. });
  75.  
  76. $(document).on("click", ".del_ExpenseRow", function(){
  77. $(this).closest('tr').remove();
  78. $lastChar = $lastChar-2;
  79. });
  80.  
  81. $("#expense_table").on("click",'.del_ExpenseRow' function(){
  82. $(this).closest('tr').remove();
  83. $lastChar = $lastChar-2;
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement