views:

298

answers:

4

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

+4  A: 

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();
tvanfosson
that's a bit redundant isn't it?
nickf
@nickf: Just trying to be consistent with the given example.
tvanfosson
A: 

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 :/

womp
+5  A: 

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
}
nickf
A: 

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

if (!$('div').is(':visible')) {
  $('div').show();
}
eKek0