Check if a div is visible

3

I need to know which div id is visible, I'm looking for isVisible () but it's not working, what should I do?

Problem:

  

I have several divs that take over the entire screen, but I only see the screen that is in question on the screen, but I need to know what id of this one. The remaining screens are property display: none;

I have the divs by default as follows:

<div class="upage hidden background_PRINCIPAL" id="ID_ESPECIFICO_DA_DIV">

and what I'm doing is the following:

if($('.upage').is(':visible') == 'home'){
}else{}
    
asked by anonymous 23.12.2015 / 16:41

1 answer

8

jQuery has a pseudo selector for this. The :visible that can be used in combination with .is() . That way you get a Boolean .

You can use this:

var visivel  = $('#minhaDiv').is(':visible');
if (visivel) alert('Sim!');
else alert('Não :( ...');

jsFiddle: link

or within the selector itself:

var elementos = $('div:visible');

In this case you will only select elements that are visible.

If you want to know the ID of several you can $('div:visible').get().map(function(el){ return this.id; }); . If it's only one you can do the same or use jQuery like this: $('div:visible').attr('id')

jsFiddle: link

    
23.12.2015 / 16:47