Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all children elements are hidden

I'm a bit stuck here with my script:

Its a checkbox filtering all .notme images and hiding it's list items. The problem is now I cant get a working callback function for the fadeToggle. It should behave like this :

If all children of #list-team-single-container are "displayed none" - do something.

$('#show-only-my-teams').change(function(){     $('.notme').each(function(){         $(this).parent().parent().fadeToggle('fast', function(){         });     }); });  
like image 815
ggzone Avatar asked Apr 30 '12 10:04

ggzone


People also ask

How do you know if an element is not visible?

You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.

How do you check if an HTML element is hidden?

Another way to check if an element is hidden is to use the window. getComputedStyle() method. It will return the display property value of the object. We can check if the string is equal to none meaning the element is hidden and do something.

How do I get the div element of all children?

If You want to get list only children elements with id or class, avoiding elements without id/class, You can use document. getElementById('container'). querySelectorAll('[id],[class]'); ... querySelectorAll('[id],[class]') will "grab" only elements with id and/or class.

How do you find the visibility of an element?

We can do the following to check an element's visibility in the viewport: Get the bounding rect of the DOM element using the getBoundingClientRect . This method returns an object with an element's width, height, and position relative to the viewport.


2 Answers

if($('#list-team-single-container').children(':visible').length == 0) {    // action when all are hidden } 
like image 110
thecodeparadox Avatar answered Sep 21 '22 22:09

thecodeparadox


The :visible jQuery selector might be what you are looking for...

From the description

Elements are considered visible if they consume space in the document. Visible elements have a width or height that is greater than zero.

Elements with visibility: hidden or opacity: 0 are considered visible, since they still consume space in the layout. During animations that hide an element, the element is considered to be visible until the end of the animation. During animations to show an element, the element is considered to be visible at the start at the animation.

http://api.jquery.com/visible-selector/


$('#list-team-single-container').children(':visible'); 

That line of code will return all of the child elements of #list-team-single-container that are visible.

$('#list-team-single-container').children(':visible').length; 

That line of code will return the number of child elements of #list-team-single-container that are visible.

like image 32
Lix Avatar answered Sep 23 '22 22:09

Lix