Marc Marc - 2 years ago 101
Javascript Question

How to remove all rows of the table but keep the header

I want to remove all rows of my table except the header.

This is what I've tried but it always deletes all rows and header:

$("#<%=tblDetailFourn.ClientID%> tbody tr").remove();

$("#<%=tblDetailFourn.ClientID%> tbody tr").not("thead tr").remove();

$("#<%=tblDetailFourn.ClientID%> tr").not("thead tr").remove();

$("#<%=tblDetailFourn.ClientID%> tbody").not("thead").remove();

$("#<%=tblDetailFourn.ClientID%> tbody").remove();

$("#<%=tblDetailFourn.ClientID%> > tbody").remove();

Here's the html:

<table id="tblDetailFourn" runat="server" class="ProjetTable ProjetTableHover">
<th style="width:200px">Rôle de Ressource</th>
<th style="width:200px">Nom Prénom</th>
<th style="width:120px">Tel</th>
<th style="width:200px">Courriel</th>
<th style="width:80px">Actif</th>
<th style="width:33px"></th>
<th style="width:33px"></th>

Answer Source

Try using this:

$('#<%=tblDetailFourn.ClientID%> tr').not(function(){ return !!$(this).has('th').length; }).remove();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download