41

I have tabs like this.

<li id="singlechatpanel-1" style="visibility: hidden;">
     //content
</li>

Trying to check it like this:

$(".subpanel a").click(function() 
     {
        var chatterNickname = $(this).text();

        if(!$("#singlechatpanel-1").is(':visible'))
        {
            alert("Room 1 is filled.");
            $("#singlechatpanel-1").css({'visibility':'visible'});
            $("#singlechatpanel-1 #chatter_nickname").html("Chatting with: " + chatterNickname);
        }

If condition always returns false. How can I check visibility state of this div?

Aristona
  • 6,763
  • 8
  • 45
  • 75

8 Answers8

53

Check if it's visible.

$("#singlechatpanel-1").is(':visible');

Check if it's hidden.

$("#singlechatpanel-1").is(':hidden');

Joshua Pinter
  • 37,288
  • 19
  • 208
  • 218
50

is(':visible') checks the display property of an element, you can use css method.

if (!$("#singlechatpanel-1").css('visibility') === 'hidden') {
   // ...
}

If you set the display property of the element to none then your if statement returns true.

undefined
  • 136,817
  • 15
  • 158
  • 186
  • One question on why we can't just use $('..').css('display') and check for none? CSS newbie here, so not sure :S – bitanath Jun 05 '17 at 10:24
  • @bitanath Please check this related question: https://stackoverflow.com/questions/133051/what-is-the-difference-between-visibilityhidden-and-displaynone – undefined Jun 06 '17 at 00:12
17

if element is hide by jquery then use

if($("#elmentid").is(':hidden'))
NorthCat
  • 8,315
  • 16
  • 40
  • 45
Shah Nawaz
  • 396
  • 2
  • 9
8

You can use .css() to get the value of "visibility":

 if( ! ( $("#singlechatpanel-1").css('visibility') === "hidden")){
 }

http://api.jquery.com/css/

Ajay2707
  • 5,345
  • 6
  • 34
  • 52
Pablo Martinez
  • 2,115
  • 1
  • 18
  • 25
3

You can use (':hidden') method to find if your div is visible or not.. Also its a good practice to cache a element if you are using it multiple times in your code..

$(".subpanel a").click(function() 
     {
        var chatterNickname = $(this).text();
        var $chatPanel = $("#singlechatpanel-1");

        if(!$chatPanel.is(':hidden'))
        {
            alert("Room 1 is filled.");
            $chatPanel.show();
            $("#singlechatpanel-1 #chatter_nickname").html("Chatting with: " + chatterNickname);
        }
});
Sushanth --
  • 53,795
  • 7
  • 57
  • 95
3
if (!$('#singlechatpanel-1').css('display') == 'none') {
   alert('visible');
}else{
   alert('hidden');
}
Prabhagaran
  • 3,070
  • 1
  • 16
  • 17
1

Add your li to a class, and do $(".myclass").hide(); at the start to hide it instead of the visibility style attribute.

As far as I know, jquery uses the display style attribute to show/hide elements instead of visibility (may be wrong on that one, in either case the above is worth trying)

PhonicUK
  • 12,412
  • 3
  • 36
  • 61
0

You can do it by any of these two ways:

$("#elementId").css('visibility')
and
$("#elementId").is(':visible');

if your element is visible then

$("#elementId").css('visibility') will return "visible"
and
$("#elementId").is(':visible') will return true

but if your element is NOT visible then

$("#elementId").css('visibility') will return undefined
and
$("#elementId").is(':visible') will return false
Khabir
  • 3,147
  • 1
  • 11
  • 18