views:

16

answers:

2

Is there a way to test if a certain element (.container) is hidden, in a whole document? Such as (which doesn't work properly):

$(".showall").click(
    function () {
        if ($(".container").is("hidden"))
                {perform a task}
            else
                {return false;}
    });
+1  A: 

It sounds like you want to test if at least one of the .container elements is hidden.

If so, you can use the :hidden selector, and check the length property to see how many were returned.

$(".showall").click(
    function () {
        if ($(".container:hidden").length)
             // found at least one hidden
        else
             // didn't find any hidden
    });

If you wanted to test to see if all were hidden, use the :visible selector like this:

$(".showall").click(
    function () {
        if ($(".container:visible").length)
             // found at least one visible
        else
             // didn't find any visible
    });
patrick dw
Why was this down voted? What is incorrect? The question isn't entirely clear. OP said *in a whole document* which sounds like OP is testing for *any* or *all* hidden or visible.
patrick dw
You were correct. The code works perfectly :)
steve
@steve - Glad it helped. :o)
patrick dw
A: 

You mean to use is visible:

$(".showall").click(
    function () {
        if ($('.container').is(":visible") == false)
                {perform a task}
            else
                {return false;}
    });
Aaron Harun