Javascript: Check Certain Check Boxes
A problem I ran with check boxes that I had four sets of checkboxes (7 checkboxes in each group) and I wanted one check box to make all those check boxes in that group checked. The added problem was that the field names were array names so they couldn’t be used and the other way was to use IDs but since you couldn’t use IDs more than once that solution was out. So I found a javascript from here: http://www.tanguay.info/web/codeExample.php?id=727
Then I modified it so it would just go through the group of checkboxes I wanted and made them checked. Here is the new javascript:
<script>
function toggleCheckedAll (boxname, id, boxnum, boxend) {
//variables
var allCheckBoxes = document.getElementById(id);
var checkStatus = allCheckBoxes.elements[boxname].checked;
//loop through all checkboxes
for (var i = boxnum; i < boxend; i++) {
//variables
var formElement = allCheckBoxes.elements[i];
var formElementName = formElement.name;
//check all but the one that we clicked
if(formElementName != ‘checkBoxAll’) {
if(formElement.checked) {
formElement.checked = checkStatus;
} else {
formElement.checked = checkStatus;
}
}
}
}
</script>
Then I have my check box that runs the script:
<input type=”checkbox” name=”checkBoxAll” onClick=”javascript:toggleCheckedAll(\’checkBoxAll\’, \’formname\’, 6, 22)” />
You will have to replace formname and set the begining check box number and the ending check box number and you are set to go. Now you can have multiple groups of check boxes checked.


