Given the follow:
<table id="myTable">
<tr> </tr>
<tr> </tr>
...
</table>
I can clear the table by doing: $("myTable").html("");
But, I'd like to instead clear all rows but the first one. Any ideas?
The jQuery function removes all the rows except the first one using the remove() method. Syntax: $('#btn').
The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements.
Select the table element and use remove() or detach() method to delete the all table rows.
The deleteRow() method removes the row at the specified index from a table. Tip: Use the insertRow() to create and insert a new row.
$('#myTable tr:gt(0)').remove()
I think you are trying to remove all rows except for table header so why don't you create th as
<table id='my Table'>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td></td>
</tr>
</table>
And Jquery as
$('#myTable tr td').parents('tr').remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With