Skip to content

Instantly share code, notes, and snippets.

@zenorocha
Created May 11, 2011 00:28
Show Gist options
  • Save zenorocha/965674 to your computer and use it in GitHub Desktop.
Save zenorocha/965674 to your computer and use it in GitHub Desktop.
Easiest “check all” ever with jQuery
/* HTML */
<fieldset>
// these will be affected by check all
<div><input type="checkbox" class="checkall"> Check all</div>
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
</fieldset>
<fieldset>
// these won't be affected by check all; different fieldset
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
</fieldset>
/* jQuery */
$(function () {
$('.checkall').click(function () {
$(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment