Skip to content Skip to sidebar Skip to footer

How Can I Test If An Element Is Already Visible?

In the HTML code, I use jQuery to hide or display an element having an ID. How can I test if an element is already visible? For example, we use show() to display an element having

Solution 1:

$('#element').is(':hidden'); // boolean

Solution 2:

If the element is visible, show() won't do anything, so it's pretty low risk. However, you can use the :visible or :hidden selector to find visible elements.

$('#myId:visible').hide();

$('#myId:hidden').show();

Using this in your selector, you can just fire off the show and hide methods and not worry about what is visible or not, because if you try to hide a :hidden element, you won't select anything so you won't do anything.

Post a Comment for "How Can I Test If An Element Is Already Visible?"