Check if a div is visible

Asked

Viewed 10,573 times

5

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

Problem:

I have several Ivs that take count of the whole screen, but I only leave visible 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 by default the Divs as follows:

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

and what I’m doing is this:

if($('.upage').is(':visible') == 'home'){
}else{}
  • 1

    .is(':visible');?

  • I don’t know about that.

  • 1

    @Renanrodrigues Sergio’s suggestion is correct, this is how you determine if a div is visible. But if you want to search for a visible div between a set of Divs, just use $(":visible") (but you probably want to filter more not to return all that is visible element in the full page, like $(".upage:visible")).

  • And to get the ID of the visible div ?

  • 1

    To catch Id do so: $("div:visible").attr("id");

  • what you are considering as Visible?

  • @durtto Utilizo what is not with Hidden, the tip of our friends solved my problem and cured my doubt. Thanks to all.

  • 2

    Yes, @Sergio’s response is very good.

Show 3 more comments

1 answer

10


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

You can use it like this:

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

jsFiddle: http://jsfiddle.net/jLk6ot27/

or inside the selector itself so:

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

in this case will only select elements that are visible.

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

jsFiddle: http://jsfiddle.net/jLk6ot27/2/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.