Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all rows from table except one with <th> tags

Tags:

jquery

If I have table with id="tbl_users" and I have

<tr>
   <th>name</th>
   <th>username</th>
   <th>password</th>
</tr>

and bellow I append data. How to empty table ( delete all rows with darta) before reloading users, but not to delete row with ? I use JQuery in project.

like image 939
Danka Avatar asked Jun 20 '11 06:06

Danka


4 Answers

If you always have the header line first, you can simply skip it:

$('#tbl_users tr').slice(1).remove();

Otherwise you can use the has method:

$('#tbl_users tr').has('td').remove();

To specifically look for rows that doesn't have a th tag, you can use the filter method:

$('#tbl_users tr').filter(function() {
  return !$(this).has('th');
}).remove();
like image 54
Guffa Avatar answered Oct 12 '22 02:10

Guffa


add tbody tag in your html and then do this-

 $('#tbl_users').find('tbody').remove();
like image 34
Vivek Avatar answered Oct 12 '22 03:10

Vivek


$('table#tbl_users').find('tr:not(:has(th))).remove();
like image 40
Connor Smith Avatar answered Oct 12 '22 02:10

Connor Smith


use this

$('#tbl_users td').parent().remove();
like image 28
CMaster Avatar answered Oct 12 '22 04:10

CMaster