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
$("#your-table-id").empty();
That's as fast as you get.
Two issues I can see here:
The empty() and remove() methods of jQuery actually do quite a bit of work. See John Resig's JavaScript Function Call Profiling for why.
The other thing is that for large amounts of tabular data you might consider a datagrid library such as the excellent DataTables to load your data on the fly from the server, increasing the number of network calls, but decreasing the size of those calls. I had a very complicated table with 1500 rows that got quite slow, changing to the new AJAX based table made this same data seem rather fast.
if you want to remove only fast.. you can do like below..
$( "#tableId tbody tr" ).each( function(){
this.parentNode.removeChild( this );
});
but, there can be some event-binded elements in table,
in that case,
above code is not prevent memory leak in IE... T-T and not fast in FF...
sorry....
You could try this...
var myTable= document.getElementById("myTable");
if(myTable== null)
return;
var oTBody = myTable.getElementsByTagName("TBODY")[0];
if(oTBody== null)
return;
try
{
oTBody.innerHTML = "";
}
catch(e)
{
for(var i=0, j=myTable.rows.length; i<j; i++)
myTable.deleteRow(0);
}
Using detach is magnitudes faster than any of the other answers here:
$('#mytable').find('tbody').detach();
Don't forget to put the tbody element back into the table since detach removed it:
$('#mytable').append($('<tbody>'));
Also note that when talking efficiency $(target).find(child)
syntax is faster than $(target > child)
. Why? Sizzle!
Elapsed Time to Empty 3,161 Table Rows
Using the Detach() method (as shown in my example above):
Using the empty() method:
this works for me :
1- add class for each row "removeRow"
2- in the jQuery
$(".removeRow").remove();