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
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Check the length property of the elements returned.
EDIT: changed hidden to visible
EDIT: Clarification on the use of the
.lengthproperty for a boolean test. Please see further explanation at the bottom of this answer.$('#some_ul li:visible').lengthreturns 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').lengthfind 0 visible elements, that is just the same as returningfalse.When you place
!behind it, it’s boolean value is reversed totrue. So if novisibleelements are found, the code in yourif()will run.It is exactly the same as doing:
…which takes the numeric value of
$('#some_ul li:visible').lengthand turns it into a boolean value using the==operator.To use
:hiddenyou would need to first get the total length, then compare it to the hidden length.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:
Here are some examples that equate to true:
If you’re curious about the effective boolean value of any value, place
!!before it.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: