DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Count Checked Boxes With [ ] (brackets) In The Name

03.13.2010
| 4074 views |
  • submit to reddit
        This function allows you to count the checkboxes selected and place the results in an element.  I believe this will also work for checkboxes without the []


To use:
<td align="center" onClick="anyCheck('adminForm','cid[]','selectedRecords');">


     <script language="javascript">
      

      function countChecked(form,ele,countTarget) {
      	var total = 0;
      	var myForm = document["getElementById"](form); 
      	var inputs = myForm[ele];     	
      	
      	var max = myForm[ele].length;
      	for (var idcount = 0; idcount < max; idcount++) {
      		if (
      	      		(inputs[idcount].checked) == true
      	      	) {
      			total += 1;
      		}
      	}
      	document.getElementById(countTarget).value = total; 
      }      
      </script>