How do I check if an element is hidden in jQuery?

+6399 votes
asked Oct 7, 2008 by philip-morton

It is possible to toggle the visibility of an element, using the functions .hide(), .show() or .toggle().

How would you test if an element is visible or hidden?

30 Answers

+1202 votes
answered Oct 7, 2008 by twernt

You can use the hidden selector:

// Matches all elements that are hidden

And the visible selector:

// Matches all elements that are visible
+7967 votes
answered Oct 7, 2008 by tsvetomir-tsonev

Since the question refers to a single element, this code might be more suitable:

// Checks for display:[none|block], ignores visible:[true|false]

Same as twernt's suggestion, but applied to a single element; and it matches the algorithm recommended in the jQuery FAQ

+307 votes
answered Jul 25, 2009 by user574889

From How do I determine the state of a toggled element?

You can determine whether an element is collapsed or not by using the :visible and :hidden selectors.

var isVisible = $('#myDiv').is(':visible');
var isHidden = $('#myDiv').is(':hidden');

If you're simply acting on an element based on its visibility, you can just include :visible or :hidden in the selector expression. For example:

 $('#myDiv:visible').animate({left: '+=200px'}, 'slow');
+427 votes
answered Jan 13, 2011 by aaronlile

None of these answers address what I understand to be the question, which is what I was searching for, "How do I handle items that have visibility: hidden?". Neither :visible nor :hidden will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):

$(".item").each(function() { if ($(this).css("visibility") == "hidden") { // handle non visible state } else { // handle visible state }
+742 votes
answered Mar 24, 2011 by mote
if ( $(element).css('display') == 'none' ){ // element is hidden

Functions don't work with the visibility attribute.

+176 votes
answered Jul 6, 2011 by pedro-rainho

The :visible selector according to the jQuery documentation:

  • 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.

Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout.

This is useful in some cases and useless in others, because if you want to check if the element is visible (display != none), ignoring the parents visibility, you will find that doing .css("display") == 'none' is not only faster, but will also return the visibility check correctly.

If you want to check visibility instead of display, you should use: .css("visibility") == "hidden".

Also take into consideration the additional jQuery notes:

Because :visible is a jQuery extension and not part of the CSS specification, queries using :visible cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use .filter(":visible").

Also, if you are concerned about performance, you should check Now you see me… show/hide performance (2010-05-04). And use other methods to show and hide elements.

+224 votes
answered Nov 25, 2011 by simon-weaver

Often when checking if something is visible or not, you are going to go right ahead immediately and do something else with it. jQuery chaining makes this easy.

So if you have a selector and you want to perform some action on it only if is visible or hidden, you can use filter(":visible") or filter(":hidden") followed by chaining it with the action you want to take.

So instead of an if statement, like this:

if ($('#btnUpdate').is(":visible"))
{ $('#btnUpdate').animate({ width: "toggle" }); // Hide button

Or more efficient, but even uglier:

var button = $('#btnUpdate');
if (":visible"))
{ button.animate({ width: "toggle" }); // Hide button

You can do it all in one line:

$('#btnUpdate').filter(":visible").animate({ width: "toggle" });
+158 votes
answered Feb 3, 2012 by webvitaly

How element visibility and jQuery works;

An element could be hidden with display:none, visibility:hidden or opacity:0. The difference between those methods:

  • display:none hides the element, and it does not take up any space;
  • visibility:hidden hides the element, but it still takes up space in the layout;
  • opacity:0 hides the element as "visibility:hidden", and it still takes up space in the layout; the only difference is that opacity lets one to make an element partly transparent;

    if ($('.target').is(':hidden')) { $('.target').show();
    } else { $('.target').hide();
    if ($('.target').is(':visible')) { $('.target').hide();
    } else { $('.target').show();
    if ($('.target-visibility').css('visibility') == 'hidden') { $('.target-visibility').css({ visibility: "visible", display: "" });
    } else { $('.target-visibility').css({ visibility: "hidden", display: "" });
    if ($('.target-visibility').css('opacity') == "0") { $('.target-visibility').css({ opacity: "1", display: "" });
    } else { $('.target-visibility').css({ opacity: "0", display: "" });

    Useful jQuery toggle methods:

    $('.click').click(function() { $('.target').toggle();
    $('.click').click(function() { $('.target').slideToggle();
    $('.click').click(function() { $('.target').fadeToggle();
+101 votes
answered Apr 21, 2012 by scorpion

One can simply use the hidden or visible attribute, like:


Or you can simplify the same with is as follows.

+160 votes
answered Apr 24, 2012 by abiy

This works for me, and I am using show() and hide() to make my div hidden/visible:

if( $(this).css('display') == 'none' ){ /* your code goes here */
} else { /* alternate logic */
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter