Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - fastest way to remove all rows from a very large table

I thought this might be a fast way to remove the contents of a very large table (3000 rows):

$jq("tbody", myTable).remove(); 

But it's taking around five seconds to complete in firefox. Am I doing something dumb (aside from trying to load 3000 rows in to a browser)? Is there faster way to do it?

like image 268
morgancodes Avatar asked Apr 06 '09 20:04

morgancodes


People also ask

How do I remove a row from a table in 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. Parameters: It accepts single parameter selector which is optional.

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 add and remove dynamic rows in jQuery?

remove() method we can dynamic add and delete row using jquery. append() method is used for append or add rows inside an HTML table and . remove() method to remove or delete table rows as well as all data inside it from the DOM dynamically with jquery.


2 Answers

$("#your-table-id").empty(); 

That's as fast as you get.

like image 89
Seb Avatar answered Sep 28 '22 10:09

Seb


It's better to avoid any kind of loops, just remove all elements directly like this:

$("#mytable > tbody").html(""); 
like image 24
gradosevic Avatar answered Sep 28 '22 10:09

gradosevic