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">
<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);

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


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: