jQuery Checkboxes: Select All, Select None, and Invert Selection
Written by Cory S.N. LaViska on December 9th, 2007
Learn how to add helpful checkbox behaviors with a few lines of jQuery.
We start off with a bunch of checkboxes:
<fieldset id="group_1">
<input type="checkbox" name="numbers[]" value="0" />
<input type="checkbox" name="numbers[]" value="1" />
<input type="checkbox" name="numbers[]" value="2" />
<input type="checkbox" name="numbers[]" value="3" />
<input type="checkbox" name="numbers[]" value="4" />
<input type="checkbox" name="numbers[]" value="5" />
<input type="checkbox" name="numbers[]" value="6" />
<input type="checkbox" name="numbers[]" value="7" />
<input type="checkbox" name="numbers[]" value="8" />
<input type="checkbox" name="numbers[]" value="9" />
</fieldset>
<input type="checkbox" name="numbers[]" value="0" />
<input type="checkbox" name="numbers[]" value="1" />
<input type="checkbox" name="numbers[]" value="2" />
<input type="checkbox" name="numbers[]" value="3" />
<input type="checkbox" name="numbers[]" value="4" />
<input type="checkbox" name="numbers[]" value="5" />
<input type="checkbox" name="numbers[]" value="6" />
<input type="checkbox" name="numbers[]" value="7" />
<input type="checkbox" name="numbers[]" value="8" />
<input type="checkbox" name="numbers[]" value="9" />
</fieldset>
Now, we add some links:
<a rel="group_1" href="#select_all">Select All</a>
<a rel="group_1" href="#select_none">Select None</a>
<a rel="group_1" href="#invert_selection">Invert Selection</a>
<a rel="group_1" href="#select_none">Select None</a>
<a rel="group_1" href="#invert_selection">Invert Selection</a>
The rel attribute is equal to the ID of the containing element of the checkbox group. In this example the containing element is a fieldset, but it could be a DIV, P, UL, etc.
Now, we add behaviors using jQuery:
<script type="text/javascript">
$(document).ready( function() {
// Select all
$("A[href='#select_all']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
return false;
});
// Select none
$("A[href='#select_none']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
return false;
});
// Invert selection
$("A[href='#invert_selection']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").each( function() {
$(this).attr('checked', !$(this).attr('checked'));
});
return false;
});
});
</script>
$(document).ready( function() {
// Select all
$("A[href='#select_all']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', true);
return false;
});
// Select none
$("A[href='#select_none']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").attr('checked', false);
return false;
});
// Invert selection
$("A[href='#invert_selection']").click( function() {
$("#" + $(this).attr('rel') + " INPUT[type='checkbox']").each( function() {
$(this).attr('checked', !$(this).attr('checked'));
});
return false;
});
});
</script>
To add the same functionality to another group of checkboxes, create more links and adjust the rel attribute accordingly:
<a rel="group_2" href="#select_all">Select All</a>
<a rel="group_2" href="#select_none">Select None</a>
<a rel="group_2" href="#invert_selection">Invert Selection</a>
<a rel="group_3" href="#select_all">Select All</a>
<a rel="group_3" href="#select_none">Select None</a>
<a rel="group_3" href="#invert_selection">Invert Selection</a>
...
<a rel="group_2" href="#select_none">Select None</a>
<a rel="group_2" href="#invert_selection">Invert Selection</a>
<a rel="group_3" href="#select_all">Select All</a>
<a rel="group_3" href="#select_none">Select None</a>
<a rel="group_3" href="#invert_selection">Invert Selection</a>
...

Comments
#1 mark on Jan 16th, 2008
#2 Thomas on Apr 21st, 2008
#3 Snowcore on Jun 2nd, 2008
#4 Michael on Jun 28th, 2008
#5 Cory S.N. LaViska on Jun 29th, 2008
#6 Michael on Jun 30th, 2008
#7 Mandy on Sep 29th, 2008
#8 Scream on Oct 6th, 2008
#9 Soaica Mircea on Oct 8th, 2008
#10 rani on Oct 13th, 2008
#11 Cristian Mora on Nov 5th, 2008