title: counting checkboxes after click using jQuery
body: <p>I need to count the number of checkboxes in a form that are 'checked' AFTER being clicked and BEFORE the form is submitted. I have a javascript function that is called when a box is clicked which changes some values in the form dynamically. But it is returning the counts and values which are present at the time the box is clicked.</p>
  
  <p>As my code is now, I have an onClick event handler on a  enclosing the checkbox and label:</p>
  
  <pre><code>&lt;div class="part" style="padding-right: 30px" onClick="runThis()"&gt;
      &lt;input id="checkbox_2" name="dog" type="checkbox" class='topdog'                              checked='checked' value="2" /&gt;
      &lt;label for="checkbox_2"&gt;Snoopy (Top Dog)&lt;/label&gt;
  &lt;/div&gt;
  
  ...
  
  
      function runThis() { 
      var dogsSelected = jQuery("input[name='dog']:checked");
      var numSelected = dogsSelected.length;
      alert("numSelected: " + numSelected);
      }
  </code></pre>
  
  <p>I am sure there is a simple solution or I am approaching this incorrectly.</p>
  
  <p>Any tips/advice are appreciated.</p>