Clear jquery table

I have an HTML table filled with a row of rows.

How can I delete all rows from a table?

+96
jquery html-table
Apr 12 '10 at 6:14
source share
10 answers

Use . remove ()

$("#yourtableid tr").remove(); 

If you want to save data for future use even after you delete it, you can use . detach ()

 $("#yourtableid tr").detach(); 

If the rows are children of the table, you can use the child selector instead of the child selector, for example

 $("#yourtableid > tr").remove(); 
+152
Apr 12 '10 at 6:15
source share

If you want to clear the data, but keep the headers:

 $('#myTableId tbody').empty(); 

The table should be formatted as follows:

 <table id="myTableId"> <thead> <tr> <th>header1</th><th>header2</th> </tr> </thead> <tbody> <tr> <td>data1</td><td>data2</td> </tr> </tbody> </table> 
+86
Oct 12 '11 at 12:33
source share

A little faster than removing each one individually:

 $('#myTable').empty() 

Technically, this will remove thead , tfoot and tbody .

+41
Apr 12 '10 at 6:19 06:19
source share

I need it:

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

It deletes all lines except the header.

+27
Mar 12 '12 at 20:11
source share

Nuclear option:

 $("#yourtableid").html(""); 

Destroys everything inside #yourtableid . Be careful with your selectors as it will destroy any html in the selector that you pass!

+11
Aug 10 '12 at 22:30
source share
 $("#employeeTable td").parent().remove(); 

This will remove all tr having td as a child. those. all lines except the heading will be deleted.

+11
Sep 12 '13 at 20:02
source share

This will delete all lines that belong to the body, thereby keeping the headers and body intact:

 $("#tableLoanInfos tbody tr").remove(); 
+3
Sep 18 '17 at 19:23
source share
  $('#myTable > tr').remove(); 
0
Oct 11 '13 at 10:30
source share

Presence of a table like this (with title and body)

 <table id="myTableId"> <thead> </thead> <tbody> </tbody> </table> 

remove each tr having a parent called tbody inside #tableId

 $('#tableId tbody > tr').remove(); 

and vice versa if you want to add to the table

 $('#tableId tbody').append("<tr><td></td>....</tr>"); 
0
May 23 '17 at 12:17
source share
 <table id="myTable" class="table" cellspacing="0" width="100%"> <thead> <tr> <th>Header 1</th> <th>Header 2</th> <th>Header 3</th> </tr> </thead> <tbody id="tblBody"> </tbody> </table> 

And delete:

 $("#tblBody").empty(); 
0
Jul 01 '17 at 17:11
source share



All Articles