Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery, how can I tell if the current object is hidden or not?

Before I call:

$('myObject').show();

I want to know if it is currently hidden or visible.

like image 575
Blankman Avatar asked Feb 04 '09 16:02

Blankman


People also ask

How do you check if an element is hidden using jQuery?

To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element.

Is show or hide in jQuery?

$(selector).hide(speed,callback); $(selector).show(speed,callback); The optional speed parameter specifies the speed of the hiding/showing, and can take the following values: "slow", "fast", or milliseconds.

Which jQuery is used to hide the current element?

jQuery hide() Method The hide() method hides the selected elements.

How do I know if a div is show or hide?

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


2 Answers

There's 2 ways to do it, that I know of:

if ($('#something').is(':hidden')) { }

or

if ($('#something').is(':visible')) { }

They should both work.

You can also do something like this:

$('#something:hidden').show();
$('#something:visible').hide();

Which will only call .show() if the item is already hidden, or only call .hide() if the item is already visible.

like image 175
Alex Fort Avatar answered Oct 23 '22 03:10

Alex Fort


You could also use the Toggle $(this).toggle();

like image 45
missaghi Avatar answered Oct 23 '22 03:10

missaghi