Search code examples
jqueryclassvisible

Jquery is(":visible") on a class


At the moment I am doing this:

bl = !$("#divModal1").is(":visible") &&
    !$("#divModal2").is(":visible") &&
    !$("#divModal3").is(":visible") &&
    !$("#divModal4").is(":visible") &&
    !$("#divModal5").is(":visible");

where divModal# are all divs who share the same class class="divModalDialog".

Is there a better way of doing this by checking the class rather than each individual one?

Basically bl must be false if one or more of these divModal# is showing.


Solution

  • From the .is documentation:

    Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

    Therefore, simply using the class name suffices, since .is(":visible") will return true if any of them are visible.

    bl = !$(".divModalDialog").is(":visible");