tags:

views:

42

answers:

3

Hi,

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.

Thanks in advance.

A: 

Why not:

$('.classUpper .closed')
Bobby Jack
This checks the two classes on the *same* element, he's checking for one class on the child, another on the parent.
Nick Craver
Good point! Edited.
Bobby Jack
@Bobby - Now it checks for a *child* class of close, rather than a parent :)
Nick Craver
OK, I fully admit: I have NO idea what the questioner is trying to do! ;)
Bobby Jack
-1 This checks for elements with class .classUpper and gets all the children with .closed. I think you meant to write: `.classUpper.closed`
Bob Fincheimer
@Bob - That's what he had originally, neither is quite there though, the OP is checking if the *parent* of a `.classUpper` is `.closed` :)
Nick Craver
@Nick so couldn't he do `$(.closed.classUpper).size() == $(.classUpper).size()`
Bob Fincheimer
@Bob: "('.closed.classUpper').size()" will always, presumably, return 0 since the structure is not quite as it might have seemed. There are .classUpper elements INSIDE .closed ones.
Bobby Jack
+3  A: 

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;
Nick Craver
are `.size()` and `.length` the same thing? I thought jQuery only had `.size()` and arrays had `.length`?
Bob Fincheimer
@Bob - A jQuery object *is* an array of DOM elements, if you [look in the source](http://github.com/jquery/jquery/blob/master/src/core.js#L180), a call to `.size()` is just a wrapper to `.length` :)
Nick Craver
i know it has an array of dom elements inside of it, but I thought the length was private, i didn't know the property was maintained by jQuery's code
Bob Fincheimer
@Bob: the [merge function](http://github.com/jquery/jquery) keeps .length up-to-date (merge is used by the main selector engine).
Bobby Jack
@Bob - A jQuery object inherits the `length` property when it inherits some methods from `Array` like `.push()`. As such, the `length` property behaves similar (but not identical) to that in `Array`. When for example `.push()` is used, the `length` property updates automatically. But in some cases as @Bobby Jack noted with `.merge()` an `Array` method is not used, so jQuery needs to update it manually. Here's a simple example: http://jsfiddle.net/SuHwx/
patrick dw
...correction, jQuery initializes `length` as it wouldn't be initialized until/unless an Array method is called.
patrick dw
$('.classUpper').parent(':not(.closed)').length;=> is really the solution that I was looking for.Great!@Thanks a lot Nick )
JoesyXHN
A: 

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

$('.classUpper.closed')
quis
That's not the structure that the questioner is working with, as Nick has pointed out (it's a very confusingly worded question!). I think the structure is something like: <div class="closed"><div class="classUpper"></div></div>
Bobby Jack