views:

74

answers:

3

I'd like to know if there's anything incorrect in the following :

    if($('#three').is(':visible')) {
        alert("visible");
    } else {
        alert("hidden");    
    }

Thanks

A: 

Make sure that #three has display attribute set to some default value. E.g. Display="none"

vikp
+3  A: 

Your code seems correct to me. However, visible selector on jQuery defines a not visible elements if:

  • They have a CSS display value of none.
  • They are form elements with type="hidden".
  • Their width and height are explicitly set to 0.
  • An ancestor element is hidden, so the element is not shown on the page.

Is it the case in your test?

Some others importants aspect regarding this selector is that elements with visibility: hidden or opacity: 0 are considered to be visible!

Also, since 1.3.2, this selector has evolved, as stated in the changelog.

romaintaz
thanks a lot sir. I wasn't aware of the fac that visibility : hidden is considered to be visible.Is there any way to find out about the visibility state of a <div> ?thanks
Anant
What you can do is *in addition* to the current test, to also check that `visible: hidden` is not applied to the element...
romaintaz
+1  A: 

Better you check this : :visible Selector

<script>
 if( $('#foo').is(':visible') ) {
    // it's visible, do something
}
else {
    // it's not visible so do something else
}

if( $('#foo').is(':hidden') ) {
    // it's hidden, do something
}
else {
    // it's not hidden so do something else
}

</script>
Pranay Rana
I just wanted to know if a layer was visible or not(in CSS terms)
Anant
@Anant- then just go to link and check
Pranay Rana
i'm sorry sir,but that's exactly what doesn't work. $('#foo').is(':visible') doesn't work with layers which hav visibility:hidden;
Anant