views:

130

answers:

4

I need to do something only if all the li's in a given ul are hidden.. this doesn't seem to do the trick. is there a way?

if ($('#some_ul li:hidden')) {
  // do something only if all 'li's in a given ul are hidden
}
+1  A: 

Check the length property of the elements returned.

if ( !$('#some_ul li:visible').length ) {
  // do something only if all 'li's in a given ul are hidden
}

EDIT: changed hidden to visible


EDIT: Clarification on the use of the .length property for a boolean test. Please see further explanation at the bottom of this answer.

$('#some_ul li:visible').length returns the number of elements found. Numeric values equate to true or false.

The ! gives you the negative of its true/false value.

So if $('#some_ul li:visible').length find 0 visible elements, that is just the same as returning false.

When you place ! behind it, it's boolean value is reversed to true. So if no visible elements are found, the code in your if() will run.

It is exactly the same as doing:

if ( $('#some_ul li:visible').length == 0 ) {
  // do something only if all 'li's in a given ul are hidden
}

...which takes the numeric value of $('#some_ul li:visible').length and turns it into a boolean value using the == operator.


To use :hidden you would need to first get the total length, then compare it to the hidden length.

var total = $('#some_ul li');

if ( $('#some_ul li:hidden').length == total ) {
  // do something only if all 'li's in a given ul are hidden
}

EDIT: In response to your comment, for clarification, there are several values that will equate to true/false in a test.

Here are some examples of values that equate to false:

var someVariable;  // someVariable is undefined, equates to false

var someVariable = '';   // someVariable is an empty string, equates to false

var someVariable = false; // someVariable is a boolean false, equates to false

var someVariable = 0;  // someVariable is number zero, equates to false

var someVariable = Number('a');  // someVariable is NaN (Not a Number), equates to false 

var someVariable = null;   // someVariable is null, equates to false

Here are some examples that equate to true:

var someVariable = "false";   // someVariable is a string, equates to true

var someVariable = 123;   // someVariable is a number greater than 0, equates to true

var someVariable = -123;   // someVariable is a number less than 0, equates to true

var someVariable = true;   // someVariable is a boolean true, equates to true

var someVariable = !false;   // someVariable is a negated value that would
                             //    otherwise be false, equates to true

If you're curious about the effective boolean value of any value, place !! before it.

alert( !!123 );   // Alerts true

The first ! converts the value to the opposite of its effective boolean value. The second ! converts the it back to its effective boolean value.

For example:

var a;  // an undefined variable

alert( a );   // alerts undefined, logically equates to 'false'

alert( !a );   // alerts the boolean opposite of undefined, which is 'true'

alert( !!a );   // alerts the converts the boolean opposite 'true' back to 'false'
patrick dw
that would work if any li is hidden.. he is asking for all li's to be hidden. so something like `!$('#some_ul li:visible').length` should be what to check for
Gaby
but checking the length also requires me to compare length count with the actual element count (hidden or visible) in order to see wether all the elements are hidden right?
Emin
@Gaby @Emin - Yeah, I updated once I saw that. Also added an update for the `:hidden` selector version. Thanks for the note.
patrick dw
is $('#some_ul li:hidden').length returning true/false???????
Emin
@Emin - No, but it is returning numerical values which effectively equate to true/false. Greater than 0 == true. 0 == false. There are several values that will effectively equate to false in a test. I'll update my answer in a minute with some examples.
patrick dw
@patrick - actually all the solutions here worked. my problem was caused because of something else and thats why I couldn't get a very simple thing work! but not only you have answered my question correctly, you also spent time and effort to explain what a beginner like me should consider as a treasure and as a reference. thank you very much for your time and effort!
Emin
@Emin - I'm very glad to hear it was helpful, and that you found your solution. :)
patrick dw
A: 

You can do like:

if ($('#some_ul li').is(':hidden')) {
  // do something only if all 'li's in a given ul are hidden
}
Sarfraz
A: 

I'm thinking this is what you want

if ( $('#some_ul li:hidden').length == $('#some_ul li').length ) {
  // do something only if all 'li's in a given ul are hidden
}
Steve
+1  A: 
$("ul:not(:has(li:visible))")

This selects those <ul> without visible <li> children. Check its length property to see if there are any at all, or call each() on it to do something to each of them.

Tomalak
I very much like the simplicity of this solution! thank you very much! vote up for that!
Emin