Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - If DIV ID is Visible

I am trying to find out of a DIV is hidden or if it is exposed.

This is the pseudocode:


if(DIV != VISIBLE) // not visible
{
  show content
}

Any JQuery Experts able to assist me?

Thanks, Robert

like image 400
RobertC Avatar asked Sep 21 '09 21:09

RobertC


4 Answers

If you're trying to just show a div which is hidden, then you don't actually need to do any checks at all:

$('#myDiv').show();

Regardless of its state beforehand, it'll end up visible now.

However, if you want to perform other actions depending on whether the content is visible or not, then you'll need to check:

if ($('#myDiv').is(":hidden")) 
// or
if ($('#myDiv:hidden').length) 
// or
if ($('#myDiv:not(:visible)')) {    // you get the idea...
    //perform your actions
}
like image 134
nickf Avatar answered Oct 18 '22 04:10

nickf


All other answers are fine, but this is just to translate your pseudocode into actual javascript code:

if (!$('div').is(':visible')) {
  $('div').show();
}
like image 44
eKek0 Avatar answered Oct 18 '22 02:10

eKek0


The following will display a DIV named myDiv if it is hidden. Note that if you want to do other stuff, you can also use each() rather than show and do other operations on $(this) inside each.

$('div#myDiv:hidden').show();
like image 4
tvanfosson Avatar answered Oct 18 '22 04:10

tvanfosson


This will test to see if you selected any hidden elements with the id "mydiv":

 if ( $("#mydiv:hidden").length > 0)
 {
    // 
 }

Edit: Simplified selector. Had to look it up :/

like image 1
womp Avatar answered Oct 18 '22 03:10

womp