Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the <tr> from a table except the first one using jquery

I am having a table and I want to remove all <tr> of the table except the first one. How can I do this.?

like image 395
kbvishnu Avatar asked Dec 01 '22 04:12

kbvishnu


2 Answers

There are several methods here, the most terse is to use the :gt() selector, like this:

$('#tableID tr:gt(0)').remove();
like image 71
Nick Craver Avatar answered Jan 25 '23 10:01

Nick Craver


Is there a special meaning to the first row? I am going to go out on a limb and say the first row is likely the row which contains the column headings.

If so, one easy option is to put the first row in a <thead> and all the body rows in a <tbody>. Then you could simply do:

$('#myTable tbody tr').remove();

This also gives your HTML more semantic meaning.

like image 37
Crast Avatar answered Jan 25 '23 11:01

Crast