jQuery Table Remove Rows Examples
Ever wanted to remove single row from table or remove multiple rows from a table using jQuery? In this article, I am going to show you with examples, how easy it is to remove / delete table row using jQuery.
How to Remove Single Row or Multiple Rows From A Table using jQuery
Following is the HTML source code I used for the examples:
HTML Source:
<html> <head> <title>jQuery Table Remove Row | Remove Multiple Rows from Table Examples</title> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> </head> <body> <table width="100%" border="0" cellspacing="0" cellpadding="0" id="my_table"> <tr> <td>1</td> <td>6</td> </tr> <tr> <td>2</td> <td>7</td> </tr> <tr> <td>3</td> <td>8</td> </tr> <tr> <td>4</td> <td>9</td> </tr> <tr> <td>5</td> <td>10</td> </tr> </table> <button id="remove_row">Remove Row</button> </body> </html>
In this example, we are going to remove table rows when the “Remove Row” button is clicked. So all code will go into the click event of the button. With that in mind, let’s take a look at how you can remove a single row and / or remove multiple rows from table using jQuery.
Example 1 (a): Remove all Rows except first row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove all rows except first row
//$("#my_table").find("tr:gt(0)").remove();
});
});
</script>Example 1 (b): Remove all Rows except first row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Alternate way to Remove all rows except first row
$("#my_table").find("tr:not(:first)").remove();
});
});
</script>Example 2: Remove only second row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove only 2nd row
$("#my_table").find("tr:eq(1)").remove();
});
});
</script>Example 3: Remove only third row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove only 3rd row
//$("#my_table").find("tr:eq(2)").remove();
});
});
</script>Example 4: Remove all Rows except second row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove all rows except second
$("#my_table").find("tr:not(:eq(1))").remove();
});
});
</script>Example 5: Remove last row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove last row
$('#my_table tr:last').remove();
});
});
</script>Example 6: Remove all Rows except last row of table using jQuery
<script type="text/javascript">
$(document).ready(function() {
$("#remove_row").click( function() {
//Remove all rows except last row
$("#my_table").find("tr:not(:last)").remove();
});
});
</script>As you see, all it takes is just 1 line of code to remove the row that you desire.
Your Turn!
Do you know of any other ways to remove table row or remove rows from table using jQuery? Share your answers by commenting below.
This is by far the best explanation i found on Table with jQuery. Was banging my head from few hours finding a solution.
Thank you mate for this.
Ashish
October 29, 2012 at 8:46 am
You are welcome! Glad I could help 🙂
Robert
November 1, 2012 at 5:57 pm