“CheckAll” with jquery

[javascript]

//This code checks/unchecks all checkboxes within the same fieldset. Simple and semantic.

//HTML Setup

//Add checkboxes however you like, just make sure they are within the same fieldset (or other element).

/*
<fieldset>
<!– these will be affected by check all –>
<div><input type="checkbox"> 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 field set –>
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
<div><input type="checkbox"> Checkbox</div>
</fieldset>
*/

$(function () {
$(‘.checkall’).click(function () {
$(this).parents(‘fieldset:eq(0)’).find(‘:checkbox’).attr(‘checked’, this.checked);
});
});
[/javascript]

Tags:

Leave a Reply