Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery empty is very slow

I am trying to empty an HTML table that has about 200 rows using $("#mytable").empty(). While it is emptying the rows, I cannot do anything else, looks like the UI is blocked.

Is this the right function to use? Can this operation be done in the background, in order to minimize the noticeable lag?

like image 849
G-Man Avatar asked Sep 24 '11 04:09

G-Man


3 Answers

I've never had that problem before, however, I would suggest this:

$("#mytable").children().detach().remove();

More than likely it is taking a while because of the clean-up jQuery does on the elements. With them detached, it may happen quicker.

like image 187
Kevin B Avatar answered Nov 06 '22 15:11

Kevin B


how about just:

document.getElementById('mytable').innerHTML = "";
like image 4
Matthew Avatar answered Nov 06 '22 15:11

Matthew


$.empty() slow if many childrens, I'm using:

var containerNode = document.getElementById("content");
while (containerNode.hasChildNodes()) {
    containerNode.removeChild(containerNode.lastChild);
}
like image 3
Serhii Bohutskyi Avatar answered Nov 06 '22 17:11

Serhii Bohutskyi