123...">

Delete all children from the table except the first?

My HTML:

<table id="user-table"> <tr><td>1</td></tr> <tr><td>2</td></tr> <tr><td>3</td></tr> </table> 

How to remove all children of a user table except the first <tr> using jQuery?

What I tried:

 $("#user-table").children().remove(); 

However, it removes all <tr> child elements, but I want the first line not to be deleted.

Here is my jQuery:

 $('#add-form').submit(function(e) { e.preventDefault(); $.post("../lib/ajax/add-user.php", $("#add-form").serialize(), function(data){ javascript:jQuery.fancybox.close(); $("#user-table").children().remove(); //$('#list-box').fadeTo(800,1,function(){ //$(this).empty(); //$(this)..append(data); //}); }); 
+4
source share
3 answers

slice() by far the fastest way you can do this:

 $('#user-table tr').slice(1).remove(); 

jsFiddle here.

+14
source

You can do it -

 $('#parentDiv > div:gt(0)').remove(); 

Demo ---> http://jsfiddle.net/aaCam/

+2
source

I also had the same problem once, and I fixed it with:

 $('#user-table > tr:not(:first)').remove(); 

You will also be ready.

+2
source

Source: https://habr.com/ru/post/1492072/


All Articles