A light-weight plugin to create checkbox lists with an "all" choice.
Does one thing, and does it right.
Multiple callbacks are available at every step of the way, allowing you to plug your business logic into the plugin.
CheckThemAll is compatible with all modern browsers, starting from IE7.
js/jquery-checkthemall.min.js
$('#checkbox_container').checkThemAll();
.master-checkbox
and .slave-checkbox
. Here's an example:
<div id="checkbox_container">
<input type="checkbox" class="master-checkbox" id="master" name="master" />
<label for="master">All</label>
<input type="checkbox" class="slave-checkbox" id="slave1" name="slave1" />
<label for="slave1">Choice 1</label>
<input type="checkbox" class="slave-checkbox" id="slave2" name="slave2" />
<label for="slave2">Choice 2</label>
<input type="checkbox" class="slave-checkbox" id="slave3" name="slave3" />
<label for="slave3">Choice 3</label>
<input type="checkbox" class="slave-checkbox" id="slave4" name="slave4" />
<label for="slave4">Choice 4</label>
<input type="checkbox" class="slave-checkbox" id="slave5" name="slave5" />
<label for="slave5">Choice 5</label>
</div>
In most cases, you won't need extra configuration for CheckThemAll to work properly. In some cases though, you might need to change the default parameters to meet your specific needs. There are two ways to set parameters:
data-
attribute in the <container>
element.Let's take an example with the masterCheckboxClass
parameter (described below), you can set it either in your container element:
<div id="checkbox_container" data-checkthemall-mastercheckboxclass="some-class">
...
</div>
Or when instantiating the plugin:
$('.checkthemall').checkThemAll({
masterCheckboxClass: 'some-class'
});
Here is the full (but short) list of available parameters, along with their default values. To use them as data-
attributes, simply prefix with data-checkthemall-
Note Parameters expecting functions cannot be used as data-
attributes.
defaults = {
masterCheckboxClass: 'master-checkbox',
slaveCheckboxClass: 'slave-checkbox',
onCheckBefore: function() { return; },
onCheckAfter: function() { return; },
onUncheckBefore: function() { return; },
onUncheckAfter: function() { return; },
onMasterCheckBefore: function() { return; },
onMasterCheckAfter: function() { return; },
onMasterUncheckBefore: function() { return; },
onMasterUncheckAfter: function() { return; },
onSlaveCheckBefore: function() { return; },
onSlaveCheckAfter: function() { return; },
onSlaveUncheckBefore: function() { return; },
onSlaveUncheckAfter: function() { return; },
}
You can hook to pre and post checking events to perform certain tasks.
Hooks receive 3 parameters :
container
: The DOM element wrapping the checkboxes.element
: The DOM element being checked or unchecked.options
: The list of options tied to the CheckThemAll instance.Available hooks are :
onCheckBefore
onCheckAfter
onUncheckBefore
onUncheckAfter
onMasterCheckBefore
onMasterCheckAfter
onMasterUncheckBefore
onMasterUncheckAfter
onSlaveCheckBefore
onSlaveCheckAfter
onSlaveUncheckBefore
onSlaveUncheckAfter
Example 1: Nothing checked on page load
Example 2: "All" checked on page load
Example 3: "Choice 1" and "Choice 3" checked on page load
Example 4: All choices checked on page load - the plugin unchecks them all, and checks the "All" checkbox instead.
Example 5: Radio button instead of checkbox
Example: Click the checkboxes to see the hooks in action.
Example: Disabling slaves when master is checked.