Skip to content

Instantly share code, notes, and snippets.

@jtarleton
Created May 8, 2013 15:40
Show Gist options
  • Save jtarleton/5541318 to your computer and use it in GitHub Desktop.
Save jtarleton/5541318 to your computer and use it in GitHub Desktop.
jQuery "Check All" / "Uncheck All" for an array of checkbox inputs
<script type="text/javascript">
function listen_for_uncheck_all($checkboxes)
{
jQuery('#uncheckalllink').unbind('click').bind('click', function(){
$checkboxes.removeAttr("checked");
jQuery('#uncheckalllink').html('Check All');
listen_for_check_all($checkboxes);
});
}
function listen_for_check_all($checkboxes)
{
jQuery('#uncheckalllink').unbind('click').bind('click', function(){
$checkboxes.attr("checked","checked");
jQuery('#uncheckalllink').html('Uncheck All');
listen_for_uncheck_all($checkboxes);
});
}
jQuery(document).ready(function(){
var $checkboxes = jQuery('.box [name="flds[]"]').filter(':input');
listen_for_uncheck_all($checkboxes);
listen_for_check_all($checkboxes);
});
</script>
<html>
<head>
<!-- The above Javascript -->
</head>
<body>
<div class="box">
<label for="foo">Foo</label><input id="foo" type="checkbox" name="flds[]" value="Foo"></input>
<a id="uncheckalllink">Uncheck All</a>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment