Skip to content

Instantly share code, notes, and snippets.

@maximishchenko
Created May 4, 2016 20:30
Show Gist options
  • Save maximishchenko/c0225452318360f373db97518e320d56 to your computer and use it in GitHub Desktop.
Save maximishchenko/c0225452318360f373db97518e320d56 to your computer and use it in GitHub Desktop.
Check/Uncheck all checkboxes in div
<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>
#Buttons
<input type="button" value="Check All" onclick="checkByParent('test', true);">
<input type="button" value="Uncheck All" onclick="checkByParent('test', false);">
#Links
<a href="#" onclick="checkByParent('test', true); return false;">Check all</a>
<a href="#" onclick="checkByParent('test', false); return false;">Uncheck all</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment