Use jQuery to check if all div's are hidden
Solution 1
You can do the check as by using selector as suggested above and to it like this:
if ( $("div.test:visible").length === 0)
$("#wrap1").hide( );
Solution 2
This snippet will loop all <div id="wrap#">
and hide them if the test are hidden.
$("div[id^='wrap']").each(function() {
var wrap = $(this);
if(wrap.children("div[class^='test']:visible").length == 0) {
wrap.hide();
} else {
wrap.show();
}
});
If you still want to keep your <div id="wrap#">
visible if there are no test at all (as in none in the markup), you can use the following modified snippet:
$("div[id^='wrap']").each(function() {
var wrap = $(this);
if(wrap.children("div[class^='test']").length > 0 &&
wrap.children("div[class^='test']:visible").length == 0) {
wrap.hide();
} else {
wrap.show();
}
});
There is no compelling reason to number classes (other than in edge cases). Your numbering complicates the above code as well as your CSS. It would be easier just to remove the numbering from test
. (You don't need it as you can always select a subset of them using :lt(index)
, :gt(index)
, :eq(index)
, :first
and :last
.
As for numbering ids, that's fine since each id must be unique.
user149109
Updated on January 12, 2020Comments
-
user149109 over 4 years
How would I check if all the div's with class test are hidden. And if they are all hidden set wrap1 to hidden. Thanks.
<div id='wrap1'> <div class="header">Header 1</div> <div class='test'><a href="#">Test 1</a></div> <div class='test'><a href="#">Test 2</a></div> <div class='test'><a href="#">Test 3</a></div> </div>
UPDATE: Thanks everyone for the really quick answers, I got it working. They were all very helpful.
-
tvanfosson almost 15 yearsNote that this would also hide the wrapper div if there were no divs with a matching class. Not sure if that's desired behavior or not.
-
Andrew Moore almost 15 years@tvanfosson: Technically if there are no
div.test
in the markup, it is the same as having nodiv.test
visible. I'm pretty sure that desired behavior. -
tvanfosson almost 15 yearsIf you only wanted the behavior when there were divs but they were all hidden, you could check to make sure the :hidden count is the same as the :visible count.
-
tvanfosson almost 15 years@Andrew -- I'm not sure. There's a semantic difference between "there are no results" and "all of the results are hidden". I'm not saying your answer is wrong, just pointing out that behavior applies in more than just the all are hidden case.
-
tvanfosson almost 15 yearsYou need to qualify this with the appropriate class test. According to the OP the wrapper should be hidden if the "testN" classed DIVs are hidden even if the "header" DIV isn't.
-
Andrew Moore almost 15 years@tvanfosson: Maybe, anyway, comparing the
:hidden
length to the:visible
length won't work as both will return 0 if there are no elements. -
Lukas Naujokaitis over 6 yearsYou can also change
if ( $("div.test:visible").length === 0)
line to theif (!$("div.test:visible").length)
.