How to delete all table rows except the first one in jquery

November 8, 2021
Jquery
How to delete all table rows except the first one in jquery

In this tutorial, you will learn how to delete/remove all the table rows except the first one in jquery.

This is usually required when we need to load new data from the ajax but we don’t want to remove the first row.

We will cover the multiple ways of doing that in jquery. Let’s get started

Solution 1:

Here is the first solution on how to remove all rows except the first one:

$(".tableselector").remove("tr:gt(0)");

In the above code, the tableselector is the selector of the table, and tr:gt(0) means removing all the rows which are greater than 0 and 0 is considered as the first row.

Solution 2:

Another way of doing this using the find method:

$(".tableselector").find("tr:gt(0)").remove();

Solution 3:

Third way of doing this using the children method:

$(".tableselector").children( 'tr:not(:first)' ).remove();

Conclusion:

Hope you have learned today on how to remove or delete all the table rows except the first one in jquery.

Let me know if you have any questions in the comments.

Keep learning, see you in the next one.

Write a Reply or Comment

Your email address will not be published. Required fields are marked *


Icon