olidev
olidev

Reputation: 20644

Check all divs have the same class or not

For example I have to do it manually like this:

function checkDivUppersClosed() {
    var allOpened = true;
    $('.classUpper').each(function (index, domEle) {
        allOpened &= $(this).parent().hasClass('closed');
    });
    return allOpened;        
}

I know that if I select $('.closed').size() will return the length as well. But in my case, some divs have the class classUpper but not at all.

Upvotes: 1

Views: 1017

Answers (3)

quis
quis

Reputation: 1190

If you just want to perform an operation on some elements that match your criteria then you can use:

$('.classUpper.closed')

Upvotes: -1

Nick Craver
Nick Craver

Reputation: 630409

To do the literal version of your current check you can do this:

return $('.classUpper').parent(':not(.closed)').length;

This would return the count that aren't closed (parents of these elements that do :not() have the closed class). You could use the number as a true/false check still, or add a === 0 to be explicit.

Though if the closed class is being added to hide them, you can use the :visible selector, like this:

return $('.classUpper:visible').length === 0;

Upvotes: 3

Bobby Jack
Bobby Jack

Reputation: 16018

Why not:

$('.classUpper .closed')

Upvotes: -1

Related Questions