Created
May 8, 2013 15:40
-
-
Save jtarleton/5541318 to your computer and use it in GitHub Desktop.
jQuery "Check All" / "Uncheck All" for an array of checkbox inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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