Skip to content Skip to sidebar Skip to footer

Jquery: Counting Visible Elements - Efficiency/speed Problems

I have some code that works fine but it's become too slow: HTML: I have a container that contains about 50 ul elements. Each ul element has a h4 heading followed by a series of li

Solution 1:

I suspect that determining if an element is visible or not is quite expensive. Consider instead adding and deleting a class to hide or show elements. Then you can select them directly based on the class, which will mostly be supported by a host getElementsByClassName or querySelectorAll method.

Solution 2:

try:

$('h4', '#container').css('display', 'none').filter(function() {
    return $(this).siblings('li:visible').length;
}).css('display', 'block');

but I agree with RobG, you'r markup is probably incorrect.

Post a Comment for "Jquery: Counting Visible Elements - Efficiency/speed Problems"