user187809
user187809

Reputation:

using jquery, how do I check if an element is visible or not?

I also need to find out all the elements inside a div, and check for their visibility. How to do it?

Upvotes: 10

Views: 13879

Answers (5)

ksk
ksk

Reputation: 311

use the $(div :visible) selector to select all visible elements in the div. you may loook at http://api.jquery.com/visible-selector/ for more details.

Upvotes: 1

user113716
user113716

Reputation: 322492

The first part of your question sounds like you want to find all the elements inside of a div. And then check for visibility.

To get all elements that are descendants of a div, use:

$('#myDiv *')

So to test each element, and act accordingly based on visibility:

$('#myDiv *').each(function() {
    if( $(this).is(':visible') ) {
        // code to run if visible
    } else {
        // code to run of not visible
    }
})

Upvotes: 15

Alex
Alex

Reputation: 2420

Use the :hidden and :visible selectors.

$("div:visible").hide();

$("div:hidden").show();

Upvotes: 1

jay
jay

Reputation: 10325

$('#myElement').is(':visible');

Will return true or false

Upvotes: 5

cletus
cletus

Reputation: 625077

You can select them using the :visible and :hidden pseudo-elements. For example, selects all the visible descendants of a <div>.

$("div :visible")...

Of you can do a test using is(). For example:

if ($("#someId").is(":visible")) { ...

Upvotes: 14

Related Questions