Reputation:
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
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
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
Reputation: 2420
Use the :hidden and :visible selectors.
$("div:visible").hide();
$("div:hidden").show();
Upvotes: 1