title: Count total children divs inside a container
body: <p>I want to count the total divs inside a container and toggle their visibilities with structure like this. Please also note that the div.content may also reside inside another nested or even nested-nested containers. That's why I handle it with jquery to add div.topmost for each topmost parent container:</p>
  
  <pre><code>&lt;div id="parent"&gt;
    &lt;div class="counter"&gt;There are 3 div.contents inside the container below&lt;/div&gt;
    &lt;div class="container"&gt;
      &lt;div class="content"&gt; 1 &lt;/div&gt;
      &lt;div class="container"&gt; &lt;!--container inside container --&gt;
        &lt;div class="content"&gt; 2 &lt;/div&gt;
        &lt;div class="content"&gt; 3 &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;div class="counter"&gt;There are 5 div.contents inside the container below&lt;/div&gt;
    &lt;div class="container"&gt;
      &lt;div class="content"&gt; 1 &lt;/div&gt;
      &lt;div class="content"&gt; 2 &lt;/div&gt;
      &lt;div class="content"&gt; 3 &lt;/div&gt;
      &lt;div class="content"&gt; 4 &lt;/div&gt;
      &lt;div class="content"&gt; 5 &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And the jquery:</p>
  
  <pre><code>  // only grab the top most container
    $('#parent &gt; .container').addClass('topmost');
      var topMost = $(".topmost");
      var totContent = topMost.children('.content').size();
  
      if (topMost.length &gt; 0) {
        topMost.before('&lt;div class="toggle"&gt;There are ' + totContent + ' div.contents inside the container below&lt;/div&gt;');
      }
  
     topMost.hide();
  
    $('#parent &gt; .counter').click(function() {
      $(this).next('.topmost').toggle();
      //alert(totContent);
       return false;
    });
  </code></pre>
  
  <p>But I can't make it work to loop for each div.counter. The counter always shows all div.content. So placing the each function is suspected to be the problem.</p>
  
  <p>Any hep would be very much appreciated.</p>
  
  <p>Thanks</p>