clear table jquery
Solution 1:
Use .remove()
$("#yourtableid tr").remove();
If you want to keep the data for future use even after removing it then you can use .detach()
$("#yourtableid tr").detach();
If the rows are children of the table then you can use child selector instead of descendant selector, like
$("#yourtableid > tr").remove();
Solution 2:
If you want to clear the data but keep the headers:
$('#myTableId tbody').empty();
The table must be formatted in this manner:
<table id="myTableId">
<thead>
<tr>
<th>header1</th><th>header2</th>
</tr>
</thead>
<tbody>
<tr>
<td>data1</td><td>data2</td>
</tr>
</tbody>
</table>
Solution 3:
Slightly quicker than removing each one individually:
$('#myTable').empty()
Technically, this will remove thead
, tfoot
and tbody
elements too.
Solution 4:
I needed this:
$('#myTable tbody > tr').remove();
It deletes all rows except the header.
Solution 5:
The nuclear option:
$("#yourtableid").html("");
Destroys everything inside of #yourtableid
. Be careful with your selectors, as it will destroy any html in the selector you pass!