Check all checkboxes within a div element

<div id="test">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
<script type="text/javascript">

function checkByParent(aId, aChecked) {
    var collection = document.getElementById(aId).getElementsByTagName('INPUT');
    for (var x=0; x<collection.length; x++) {
        if (collection[x].type.toUpperCase()=='CHECKBOX')
            collection[x].checked = aChecked;
    }
}
checkByParent('test', true);
</script>

example usage…

<input type="button" value="Check All" onclick="checkByParent('test', true);">
<input type="button" value="Uncheck All" onclick="checkByParent('test', false);">

or if they’re clicking on a link

<a href="index.html" onclick="checkByParent('test', true); return false;">check all</a>

Author: Guru Wannabe

Advertisements