jQuery Snippets: Check/Un-Check All Function
This easy-to-implement jQuery code snippet is super useful, and can be used to create a functionality that will check OR uncheck any checkboxes within a particular fieldeset. So for example, if your HTML looks like this:
<fieldset> <div><input type=“checkbox” class=“checkall”>Check All</div> <div><input type=“checkbox”>Check Me</div> <div><input type=“checkbox”>Check Me</div> <div><input type=“checkbox”>Check Me</div> </fieldset>
Your jQuery code to make the .checkall class actually check all of the checkbox inputs should look something like this:
$(function() { $('.checkall').click(function() { $(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked); }); });
With the above code, you can easily add a “Check All” button to any of your fieldset elements.