JavascriptToolbox.com Now Available!

Almost all of my javascript code has been moved over to its new home at The Javascript Toolbox. Please go there to find the latest scripts, information, etc. These pages will remain here for a while for historical purposes in case anyone needs a production copy of old code.
CheckBoxGroup
[Toolbox]  [Example]  [Source]
Click the tabs to see other source options.
  Original     Compact  
Source file with comments removed and whitespace reduced (4k)
Right-click and "Save As" to download this file: CheckBoxGroup.js or copy and paste the text from below.

   
PLEASE SUPPORT THIS SITE! Rather than charging money for my Javascript, I rely on donations from users to help support the site and offset development time. If you find this code useful or if it helped on your project, please consider donating some money via PayPal. Any amount (even $1) is appreciated! Thanks!

// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. 
// If you wish to share this code with others, please just point them
// to the URL instead.
// Please DO NOT link directly to my .js files from your site. Copy
// the files to your server and use them there. Thank you.
// ===================================================================

function CheckBoxGroup(){this.controlBox=null;this.controlBoxChecked=null;this.maxAllowed=null;this.maxAllowedMessage=null;this.masterBehavior=null;this.formRef=null;this.checkboxWildcardNames=new Array();this.checkboxNames=new Array();this.totalBoxes=0;this.totalSelected=0;this.setControlBox=CBG_setControlBox;this.setMaxAllowed=CBG_setMaxAllowed;this.setMasterBehavior=CBG_setMasterBehavior;this.addToGroup=CBG_addToGroup;this.expandWildcards=CBG_expandWildcards;this.addWildcardCheckboxes=CBG_addWildcardCheckboxes;this.addArrayCheckboxes=CBG_addArrayCheckboxes;this.addSingleCheckbox=CBG_addSingleCheckbox;this.check=CBG_check;}
function CBG_setControlBox(name){this.controlBox=name;}
function CBG_setMaxAllowed(num,msg){this.maxAllowed=num;if(msg!=null&&msg!=""){this.maxAllowedMessage=msg;}}
function CBG_setMasterBehavior(b){this.masterBehavior = b.toLowerCase();}
function CBG_addToGroup(){if(arguments.length>0){for(var i=0;i<arguments.length;i++){this.checkboxWildcardNames[this.checkboxWildcardNames.length]=arguments[i];}}}
function CBG_expandWildcards(){if(this.formRef==null){alert("ERROR: No form element has been passed.  Cannot extract form name!");return false;}for(var i=0;i<this.checkboxWildcardNames.length;i++){var n = this.checkboxWildcardNames[i];var el = this.formRef[n];if(n.indexOf("*")!=-1){this.addWildcardCheckboxes(n);}else if(CBG_nameIsArray(el)){this.addArrayCheckboxes(n);}else{this.addSingleCheckbox(el);}}}
function CBG_addWildcardCheckboxes(name){var i=name.indexOf("*");if((i==0) ||(i==name.length-1)){searchString=(i)?name.substring(0,name.length-1):name.substring(1,name.length);for(var j=0;j<this.formRef.length;j++){currentElement = this.formRef.elements[j];currentElementName=currentElement.name;var partialName =(i)?currentElementName.substring(0,searchString.length) : currentElementName.substring(currentElementName.length-searchString.length,currentElementName.length);if(partialName==searchString){if(CBG_nameIsArray(currentElement)) this.addArrayCheckboxes(currentElement);else this.addSingleCheckbox(currentElement);}}}}
function CBG_addArrayCheckboxes(name){if((CBG_nameIsArray(this.formRef[name])) &&(this.formRef[name].length>0)){for(var i=0;i<this.formRef[name].length;i++){this.addSingleCheckbox(this.formRef[name][i]);}}}
function CBG_addSingleCheckbox(obj){if(obj != this.formRef[this.controlBox]){this.checkboxNames[this.checkboxNames.length]=obj;this.totalBoxes++;if(obj.checked){this.totalSelected++;}}}
function CBG_check(obj){var checked=obj.checked;if(this.formRef==null){this.formRef=obj.form;this.expandWildcards();if(this.controlBox==null || obj.name!=this.controlBox){this.totalSelected +=(checked)?-1:1;}}if(this.controlBox!=null&&obj.name==this.controlBox){if(this.masterBehavior=="all"){for(i=0;i<this.checkboxNames.length;i++){this.checkboxNames[i].checked=checked;}this.totalSelected=(checked)?this.checkboxNames.length:0;}else{if(!checked){obj.checked =(this.totalSelected>0)?true:false;obj.blur();}}}else{if(this.masterBehavior=="all"){if(!checked){this.formRef[this.controlBox].checked=false;this.totalSelected--;}else{this.totalSelected++;}if(this.controlBox!=null){this.formRef[this.controlBox].checked=(this.totalSelected==this.totalBoxes)?true:false;}}else{if(!obj.checked){this.totalSelected--;}else{this.totalSelected++;}if(this.controlBox!=null){this.formRef[this.controlBox].checked=(this.totalSelected>0)?true:false;}if(this.maxAllowed!=null){if(this.totalSelected>this.maxAllowed){obj.checked=false;this.totalSelected--;if(this.maxAllowedMessage!=null){alert(this.maxAllowedMessage);}return false;}}}}}
function CBG_nameIsArray(obj){return((typeof obj.type!="string")&&(obj.length>0)&&(obj[0]!=null)&&(obj[0].type=="checkbox"));}