Check the length property of the returned elements.
if ( !$('#some_ul li:visible').length ) {
EDIT: hidden on visible
EDIT: Explained the use of the .length property for boolean testing. Please see further explanation at the bottom of this answer.
$('#some_ul li:visible').length returns the number of elements found. Numeric values ββare true or false.
! gives a negative value of its true / false value.
So, if $('#some_ul li:visible').length find 0 visible elements, this is the same as returning false .
When you place ! behind it, this boolean will change to true . Therefore, if no visible elements are found, the code in your if() will be launched.
This is exactly the same as doing:
if ( $('#some_ul li:visible').length == 0 ) {
... which takes the numeric value of $('#some_ul li:visible').length and turns it into a boolean using the == operator.
To use :hidden , you first need to get the total length, and then compare it with the hidden length.
var total = $('#some_ul li'); if ( $('#some_ul li:hidden').length == total ) {
EDIT: In response to your comment, for clarification, there are several values ββthat will be true / false in the test.
Here are some examples of values ββthat are 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 a few 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 of any value, put !! in front of him.
alert( !!123 ); // Alerts true
First one ! converts a value as opposed to its effective logical value. Second one ! converts it back to its effective boolean value.
For instance:
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'