Last Updated on June 1, 2021 by Roshan Parihar
In this tutorial, learn how to add/remove table rows dynamically using jQuery. Add table rows on button click and remove the added rows after addition.
After you add the rows, you can remove only those rows which you want to remove from the table. Add and delete rows dynamically after entering the data in the input box for each row.
How to Add Table Rows Dynamically Using jQuery
If you want to add rows dynamically, you can perform this task using jQuery. You can use input boxes and the required table heading for the first row.
The input boxes are helpful to insert the table rows with its values. You have to use the jQuery append() to append the new rows to the table with values on button click.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<style> table.myTable{ width: 100%; border-collapse: collapse; margin-top:20px; } table.myTable th, table.myTable td{ border: 1px solid #cdcdcd; padding: 7px; } </style> <script> $(document).ready(function(){ $('.btn-addrow').click(function(){ var txtTeam = $('.txtTeam').val(); var txtCricketer = $('.txtCricketer').val(); var txtScore = $('.txtScore').val(); $('.myTable tbody').append("<tr><td>"+txtTeam+"</td><td>"+txtCricketer+"</td><td>"+txtScore+"</td></tr>"); }); }); </script> <p>Please fill the below form and click the 'Add New Row' button to add table rows dynamically.</p> <form> <input type="text" class="txtTeam" placeholder="Enter Team Name"> <input type="text" class="txtCricketer" placeholder="Enter Cricketer Name"> <input type="text" class="txtScore" placeholder="Enter Score"> <button type="button" class="btn-addrow">Add New Row</button> </form> <table class="myTable"> <thead> <tr> <th>Team Name</th> <th>Cricketer Name</th> <th>Score</th> </tr> </thead> <tbody> </tbody> </table> |
Output
Please fill the below form and click the ‘Add New Row’ button to add table rows dynamically.
Team Name | Cricketer Name | Score |
---|
The above example contains the input boxes for each cell of a new row of the table. Enter your required values in the input boxes given above and click the ‘Add New Row‘ button. The new row gets added after the button click.
However, the above example contains no functionality to remove rows after addition. To remove rows of a table after addition, read further.
Remove Table Rows Dynamically Using jQuery
To remove the table rows, you have to use the jQuery remove(). Each row of a table contains the delete button on click.
You can choose your required row you want to delete from the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<style> table.myTableDelete{ width: 100%; border-collapse: collapse; margin-top:20px; } table.myTableDelete th, table.myTableDelete td{ border: 1px solid #cdcdcd; padding: 7px; } </style> <script> $(document).ready(function(){ $('.btndeleterow').click(function(){ $(this).closest('tr').remove(); }); }); </script> <p>Click the cross(X) sign button to remove the unwanted rows of a table.</p> <table class="myTableDelete"> <thead> <tr> <th>Team Name</th> <th>Cricketer Name</th> <th>Score</th> <th>Delete</th> </tr> </thead> <tbody> <tr> <td>West Indies</td> <td>Chris Gayle</td> <td>195</td> <td><button type="button" class="btndeleterow" >x</button></td> </tr> <tr> <td>India</td> <td>Virat Kohli</td> <td>124</td> <td><button type="button" class="btndeleterow">x</button></td> </tr> <tr> <td>Australia</td> <td>David Warner</td> <td>167</td> <td><button type="button" class="btndeleterow">x</button></td> </tr> <tr> <td>South Africa</td> <td>AB De Villier's</td> <td>185</td> <td><button type="button" class="btndeleterow">x</button></td> </tr> </tbody> </table> |
Output
Click the cross(X) sign button to remove the unwanted rows of a table.
Team Name | Cricketer Name | Score | Delete |
---|---|---|---|
West Indies | Chris Gayle | 195 | |
India | Virat Kohli | 124 | |
Australia | David Warner | 167 | |
South Africa | AB De Villier’s | 185 |
The above example contains the table with 4 rows. Click the delete button of the row which you want to remove from the table.
You can delete rows dynamically with the method given above. However, if you want to perform both addition and deletion in the same table, read further.
Dynamically Adding and Deleting Rows Using jQuery
You can perform both add and remove table rows dynamically with jQuery. To add and remove rows in the same table, you have to add a delete button on the addition of the rows.
The logic is same, you have to use the input boxes to get the entries of data for each row. Also, you have to add a button with each input of rows using jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<style> table.TableAddDelete{ width: 100%; border-collapse: collapse; margin-top:20px; } table.TableAddDelete th, table.TableAddDelete td{ border: 1px solid #cdcdcd; padding: 7px; } </style> <script> $(document).ready(function(){ $('.btnrowAddDelete').click(function(){ var txtTeamAddDelete = $('.txtTeamAddDelete').val(); var txtCricketerAddDelete = $('.txtCricketerAddDelete').val(); var txtScoreAddDelete = $('.txtScoreAddDelete').val(); $('.TableAddDelete tbody').append("<tr><td>"+txtTeamAddDelete+"</td><td>"+txtCricketerAddDelete+"</td><td>"+txtScoreAddDelete+"</td><td><button type='button' class='btndeleterowadded btn'>x</button></td></tr>"); }); $(document).on('click', 'button.btndeleterowadded', function(){ $(this).closest('tr').remove(); return false; }); }); </script> <form> <input type="text" class="txtTeamAddDelete" placeholder="Enter Team Name"> <input type="text" class="txtCricketerAddDelete" placeholder="Enter Cricketer Name"> <input type="text" class="txtScoreAddDelete" placeholder="Enter Score"> <button type="button" class="btnrowAddDelete">Add New Row</button> </form> <table class="TableAddDelete"> <thead> <tr> <th>Team Name</th> <th>Cricketer Name</th> <th>Score</th> <th>Delete</th> </tr> </thead> <tbody> </tbody> </table> |
Output
Team Name | Cricketer Name | Score | Delete |
---|
The above example contains the input boxes to get data from users for each row. Add a new row by putting the value in the input boxes and click the add button. The delete button also gets added to each entry.
Now, if you want to delete rows of a table dynamically. Click the delete button for the row you want to delete from the table.
You may also like to read
- Color Alternate Table Rows Using Boostrap
- Highlight Table Row On Hover Mouse Using CSS
- Delete All Table Rows Except First Using JQuery
Hope, you like this tutorial of how to add/remove table rows dynamically using jQuery. If you have any query regarding the tutorial, please comment below.
References
Also tell me, which method are you using to add and remove the table rows with jQuery.
The ‘Add New Row‘ button is not working.
Honey, I have made the correction in the above codes. The ‘Add New Row‘ button is working now, please check it again. Thanks for your suggestion.
That’s really nice way to add and remove the table rows. Since I was already using jQuery it didn’t take me a long time to implement, thanks.
You’re most welcome Atul