Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.toggle() extremely slow on many <TR> elements

Tags:

html

jquery

I have a table like this:

<table>
    <tr class="a"><td></td></tr>
    <tr class="b"><td></td></tr>
</table>

There are nearly 800 rows and most of them of class a. Now I want to toggle these rows like this:

    $("#toggle_a").click(function(){
        $("tr.a").toggle();
    });
    $("#toggle_b").click(function(){
        $("tr.b").toggle();
    });

But this is really extremely slow and most of the time the browser wants to stop the action.

Has anybody an idea how to make this thing faster and usable?

like image 268
adnek Avatar asked Dec 20 '10 10:12

adnek


2 Answers

Seems because jquery searching element by class name..

Note: The class selector is among the slowest selectors in jQuery; in IE it loops through the entire DOM. Avoid using it whenever possible.

Also check this article

like image 112
Andrew Orsich Avatar answered Sep 22 '22 00:09

Andrew Orsich


I had a similar problem but with few lines, about 200.

I used .hide() and .show() and now this almost instantaneous.

like image 44
Richard Avatar answered Sep 23 '22 00:09

Richard