0

I am trying to get status of a div visibility , its display is block or none here is my code

 $('#btn').live('click', function (event) {

      var status = $('#menuDiv').is(":visible");
         alert(status);

 });

but it always return false even div is visible Here is the fiddle http://jsfiddle.net/50at8ydj/4/

AddyProg
  • 2,960
  • 13
  • 59
  • 110
  • 2
    you have a typo in your fiddle = id-menuDiv instead of id=menuDiv (same for the other id) - just change that and it'll work – matthias_h Aug 24 '14 at 19:21
  • Set proper ids in your fiddle. – u_mulder Aug 24 '14 at 19:21
  • well that's embracing .. sorry guys ... – AddyProg Aug 24 '14 at 19:23
  • @matthias_h can u plz look at the updated fiddle after putting toggle i am getting true even if div is hidden – AddyProg Aug 24 '14 at 19:27
  • 1
    @AdilWaqar sure; btw the id-"contentDiv" should be id="contentDiv", and for the result of :visible in combination with toggle just have a look here: http://stackoverflow.com/questions/1345652/slidetoggle-and-visible – matthias_h Aug 24 '14 at 19:32

4 Answers4

0
  if ( $('#menuDiv').css('visibility') == 'hidden' )
  { 
     ...
  }
  else { ...}
T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

Why not just checking: $('#menuDiv').css("display") === 'none' (or ==='block', depends on your need/common case)

so simple....

Ziv Levy
  • 1,904
  • 2
  • 21
  • 32
0

Hoping to help You consider to change Your function to:

jQuery('#btn').click(function (event) {
            var status = $('#menuDiv').is(":visible");
            $('#menuDiv').toggle(!status);
            alert(status);
});

In practice, the toggle must accept a boolean value "showOrHide" indicating whether to show or hide the elements. For more info see http://api.jquery.com/toggle/#toggle-showOrHide

bye

gaetanoM
  • 41,594
  • 6
  • 42
  • 61
0

Actually you just have a typo..

In HTML you wrote id- instead of id=

Fiddle

Tomzan
  • 2,808
  • 14
  • 25