I want to count the total number of divs inside a container and switch their visibility with a structure like this. Also note that div.content can also be inside other nested or even nested nested containers. This is why I process it with jquery to add div.topmost for each topmost parent container:
<div id="parent">
<div class="counter">There are 3 div.contents inside the container below</div>
<div class="container">
<div class="content"> 1 </div>
<div class="container">
<div class="content"> 2 </div>
<div class="content"> 3 </div>
</div>
</div>
<div class="counter">There are 5 div.contents inside the container below</div>
<div class="container">
<div class="content"> 1 </div>
<div class="content"> 2 </div>
<div class="content"> 3 </div>
<div class="content"> 4 </div>
<div class="content"> 5 </div>
</div>
</div>
And jquery:
$('#parent > .container').addClass('topmost');
var topMost = $(".topmost");
var totContent = topMost.children('.content').size();
if (topMost.length > 0) {
topMost.before('<div class="toggle">There are ' + totContent + ' div.contents inside the container below</div>');
}
topMost.hide();
$('#parent > .counter').click(function() {
$(this).next('.topmost').toggle();
return false;
});
But I can't get it to work for a loop for each div.counter. The counter always shows all div.content. Thus, it is believed that the problem is associated with each function.
Any hep would be greatly appreciated.
thank
source
share