1<script language="JavaScript">
2function toggle(source) {
3 checkboxes = document.getElementsByName('foo');
4 for(var i=0, n=checkboxes.length;i<n;i++) {
5 checkboxes[i].checked = source.checked;
6 }
7}
8</script>
9
10<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
11
12<input type="checkbox" name="foo" value="bar1"> Bar 1<br/>
13<input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
14<input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
15<input type="checkbox" name="foo" value="bar4"> Bar 4<br/>
16
1<script language="JavaScript">
2function toggle(source) {
3 checkboxes = document.getElementsByName('foo');
4 for(var checkbox in checkboxes)
5 checkbox.checked = source.checked;
6}
7</script>
8
9<input type="checkbox" onClick="toggle(this)" /> Toggle All<br/>
10
11<input type="checkbox" name="foo" value="bar1"> Bar 1<br/>
12<input type="checkbox" name="foo" value="bar2"> Bar 2<br/>
13<input type="checkbox" name="foo" value="bar3"> Bar 3<br/>
14<input type="checkbox" name="foo" value="bar4"> Bar 4<br/>
15
1function setAllCheckboxes(divId, sourceCheckbox) {
2 divElement = document.getElementById(divId);
3 inputElements = divElement.getElementsByTagName('input');
4 for (i = 0; i < inputElements.length; i++) {
5 if (inputElements[i].type != 'checkbox')
6 continue;
7 inputElements[i].checked = sourceCheckbox.checked;
8 }
9}
10
1<p><input onClick="setAllCheckboxes('actors', this);" type="checkbox" />All of them</p>
2<div id="actors">
3 <p><input type="checkbox" name="kevin" />Spacey, Kevin</p>
4 <p><input type="checkbox" name="colin" />Firth, Colin</p>
5 <p><input type="checkbox" name="scarlett" />Johansson, Scarlett</p>
6</div>
7