-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.checkgroup.js
80 lines (73 loc) · 2.01 KB
/
jquery.checkgroup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
(function($){
$.fn.checkgroup = function(options){
// use a closure to allow this to be called for multiple groups
(function(ctrl_box, options) {
//merge settings
var settings=$.extend({
groupSelector:null,
groupName:'group_name',
enabledOnly:false,
onComplete:null,
onChange:null
},options || {});
//allow a group selector override option
var grp_slctr = (settings.groupSelector==null) ? 'input[name='+settings.groupName+']' : settings.groupSelector;
//setup the callback functions
var _onComplete = settings.onComplete;
var _onChange = settings.onChange;
//internal functions
var _ctrl_box_autoenable = function (){
//if # of chkbxes is equal to # of chkbxes that are checked
if($(grp_slctr).size()==$(grp_slctr+':checked').size()){
ctrl_box.attr('checked','checked');
}
}
var _ctrl_box_autodisable = function(){
ctrl_box.attr('checked',false);
}
//grab only enabled checkboxes if required
if(settings.enabledOnly)
{
grp_slctr += ':enabled';
}
//attach click event to the "check all" checkbox(s)
ctrl_box.click(function(e){
var chk_val=(e.target.checked);
//check the boxes and prepare for callback;
var boxes=Array();
var $i=0;
$(grp_slctr).each(function(){
if (this.checked!=chk_val) {
boxes[$i] = this;
this.checked=chk_val;
if (typeof _onChange == "function") {
_onChange(this);
}
$i++;
}
});
//if there are other "select all" boxes, sync them
ctrl_box.attr('checked',chk_val);
if(typeof _onComplete == "function")
{
_onComplete(boxes);
}
});
//attach click event to checkboxes in the "group"
$(grp_slctr).click(function(){
if(!this.checked)
{
_ctrl_box_autodisable();
}
else
{
_ctrl_box_autoenable();
}
});
//initialize
_ctrl_box_autoenable();
}(this, options));
//make this function chainable within jquery
return this;
};
})(jQuery);