Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery clear all rows after the first row?

Tags:

jquery

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?

like image 644
john Avatar asked Feb 23 '11 16:02

john


People also ask

How we can delete all table rows except first one using jQuery?

The jQuery function removes all the rows except the first one using the remove() method. Syntax: $('#btn').

How to clear data in table using jQuery?

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.

How do I remove all TR from a table?

Select the table element and use remove() or detach() method to delete the all table rows.

How do you delete a row in a table using Javascript?

The deleteRow() method removes the row at the specified index from a table. Tip: Use the insertRow() to create and insert a new row.


2 Answers

$('#myTable tr:gt(0)').remove()
like image 114
Jeff Avatar answered Oct 23 '22 08:10

Jeff


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();
like image 2
Harsh Baid Avatar answered Oct 23 '22 09:10

Harsh Baid