E-Learning

Learn JavaScript – How To Check if An Element is Hidden Using jQuery

 
When we are using jQuery in JavaScript, often we want to take an action based on the state of an element. One that is widely used is if the state of the element is visible or not. An element is visible when if it consumes space in the document. Visible elements have a height or width larger than zero.

To find out if an element is hidden, we basically have two tools to use. The first that we’ll see use is: visible.
 

JavaScript – using: visible with jQuery

The :visible selector checks whether the element is visible within the DOM.

$ (Document) .ready (function () {
    if ($ ( "# content") is (. ": visible"))
    {
        alert ( 'element is visible in the DOM');
    }
    Else
    {
        alert ( 'element is not visible in the DOM');
    }
});

Below we have an example using this selector to determine an action within the program:

<Div id = "countries">
  <Div id = "p1"> US </ div>
  <Div style = "display: none" id = "p2"> Canada </ div>
  <Div id = "p3"> Mexico </ div>
</div>

To check if the div #countires is visible, use the is () function with the selector “: visible”:

if ($ ( '# countries') is (. ': visible')) {
  // Is visible, do something
Else
  // Is not visible, do something
}

Loop the visible divs inside the div #countries:

$ ( "# Div countries: visible"). Each (function () {
  document.write ($ (this) .html ()   '<br />');
});

The result will be:

USA
Mexico

 

JavaScript – using: hidden in jQuery

The :hidden selector perform the opposite check of the previous one. It checks if the element is hidden inside the DOM

var isHidden = $ ( "#myDiv" ) .is ( ": Hidden" );

With these two selectors, you can verify the state of the element and perform something within the code based on the response. Do you know other ways to do this operation in JavaScript?  Share your comments in the section below!

If you want to explore other questions, you can check our videos on JavaScript. Below are some examples:

You can also subscribe to some channels that broadcast in JavaScript, such as the following:

ObsidianBlk

 TimMikeladze

Another cool way to find out interesting things about JavaScript is to access our project page!

Dr. Michael J. Garbade

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.

View Comments

Recent Posts

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

1 month ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

3 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

6 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

7 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

8 months ago

Highest Stable Coin Yields – (W12 – 2024)

It’s another week and like always we have to explore the top yield platforms for…

8 months ago