jQuery Check All Checkbox
by John Pasquet Wednesday, August 17th, 2011
The Problem
Create a “select all” checkbox that
- when checked or unchecked, applies its value to all the checkboxes defined as its children
- when a child checkbox is checked or unchecked, the “select all” checkbox is updated accordingly. So, if you click the “select all”, but then uncheck one child checbox, deselect the “select all” checkbox.
The Solution
initSelectAll: function ($chkAll, $checkboxes) {
//when chkAll is clicked, update the other checkboxes with its value
$chkAll.click(function () {
$checkboxes.attr("checked", $chkAll.attr("checked"));
});
// when any child checkbox is checked or unchecked reset the chkAll value
// chkAll will be checked if number of checked checkboxes = total number of checkboxes.
$checkboxes.click(function () {
$chkAll.attr("checked", $checkboxes.filter(":checked").length == $checkboxes.length);
}
}
$chkAll = $("#chkAll"); // or however you select the select all checkbox
$checkboxes = $("#checkboxes :checkbox");
$(function () {
initSelectAll($chkAll, $checkboxes);
});
Tags: Checkbox, jQuery, select all checkbox
« Setting ‘Enable 32-Bit Applications’ To Fix Microsoft.Jet.OLEDB Error Scrollable DataGrid table with Fixed Header with jQuery »


October 27th, 2011 at 1:20 pm
[...] the select/deselect all functionality. See this blog post in reference to the :asp() selector, and this post for more information on the select all functionality. Note: we’re not using selectAllInTable [...]