jQuery: how to check if an element is hidden? (guide for beginners)

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: why this question concerns every frontend developer

\n\n

Imagine a situation: you are writing an interactive page with a dropdown menu, modal window, or accordion. The user clicks a button, and the block should smoothly appear or disappear. But what if you accidentally try to animate an already hidden element? Or, conversely, show something that is already visible? The result — bugs, a jittery interface, and disappointed users.

\n\n

That is why the question "How do I check if an element is hidden in jQuery?" is one of the most popular on Stack Overflow. It has over a thousand answers, and this is no coincidence. The ability to correctly determine element visibility is a basic skill that will save you hours of debugging.

\n\n

In this article, we will thoroughly examine how to check if an element is hidden using jQuery. You will learn about pitfalls, differences between display:none and visibility:hidden, and how to write reliable code. Let's go!

\n\n

The .is(':hidden') method — your main tool

\n\n

The simplest and most common way to check if an element is hidden is to use the .is() method with the :hidden pseudo-selector. jQuery provides it out of the box.

\n\n

How it works

\n

The :hidden selector in jQuery selects elements that:

\n
    \n
  • Have the CSS property display: none;
  • \n
  • Are elements of type <input type="hidden">;
  • \n
  • Have width and height equal to zero (e.g., width: 0; height: 0);
  • \n
  • Are descendants of a hidden parent (inherit invisibility).
  • \n
\n\n

Here is what a simple check looks like:

\n\n
// Check if the element with id="myDiv" is hidden\nif ($('#myDiv').is(':hidden')) {\n    console.log('Element is hidden!');\n} else {\n    console.log('Element is visible.');\n}
\n\n

Checking visibility with .is(':visible')

\n

For the reverse logic, use the :visible selector:

\n\n
if ($('#myDiv').is(':visible')) {\n    console.log('Element is visible on the page.');\n}
\n\n

Both approaches work quickly and are intuitive. But there is a nuance: what if the element is hidden using visibility: hidden or opacity: 0? jQuery considers such elements visible, which often comes as a surprise to beginners.

\n\n

Pitfalls: visibility: hidden and opacity: 0

\n\n

Many developers mistakenly believe that :hidden responds to any method of hiding an element. This is not the case.

\n\n

Example with visibility: hidden

\n

The visibility: hidden property makes an element invisible, but it still occupies space on the page. jQuery considers such an element visible because its dimensions are not zero.

\n\n
<div id="myBox" style="visibility: hidden;">I am invisible but take up space</div>\n\n<script>\nconsole.log( $('#myBox').is(':hidden') );  // false\nconsole.log( $('#myBox').is(':visible') ); // true\n</script>
\n\n

Example with opacity: 0

\n

The same applies to transparency:

\n\n
<div id="transparentBox" style="opacity: 0;">Transparent, but visible to jQuery</div>\n\n<script>\nconsole.log( $('#transparentBox').is(':hidden') );  // false\n</script>
\n\n

If your code uses fadeIn()/fadeOut() or transparency animation, the standard :hidden check will let you down. For such cases, you need to check the specific CSS property.

\n\n

How to check for visibility: hidden or opacity: 0

\n\n

When you need to find out if an element is hidden specifically using these properties, use .css():

\n\n

Checking visibility

\n
var isVisibilityHidden = ($('#myElement').css('visibility') === 'hidden');\nif (isVisibilityHidden) {\n    console.log('Element is hidden via visibi                

Blogs

Book Recommendations