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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: