This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.selectall.js
53 lines (51 loc) · 1.89 KB
/
jquery.selectall.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
/**
* selectAll jQuery plugin by Jonathan Dean (http://jonathandean.com)
* With contributions by Brian Cavalier (http://briancavalier.com/)
*
* Turns a checkbox into one that selects or deselects other checkboxes on the page
*
* Options:
* group: Selector specifying a parent element of the checkboxes the selectAll checkbox should manipulate
* row: Selector for the checkboxes the selectAll checkbox should manipulate
*
* Copyright (c) 2010 Jonathan Dean (http://jonathandean.com)
* Dual licensed under the MIT (MIT-LICENSE.txt)
* and GPL (GPL-LICENSE.txt) licenses.
*
* Built on top of the jQuery library
* http://jquery.com
*/
(function($, window){
$.fn.selectAll = function(options){
if(typeof options == 'undefined'){
options = {};
}
var defaults = {
/* warning: these default selectors are likely to be very ineffecient. try to make them more specific */
group: 'body',
row: 'input[type="checkbox"]',
onchange: function(checked, unchecked) {}
},
opts = $.extend(defaults, options),
self = this,
checkedItems = opts.row + ":checked",
uncheckedItems = opts.row + ":not(:checked)";
function onChange(checked, unchecked) {
// Fire registered onchange handler
opts.onchange(checked, unchecked);
}
return this.each(function() {
$(this).on('change', function(){
var other_rows = $(opts.group).find(opts.row).not(this);
var checked = $(this).is(':checked');
other_rows.prop('checked', checked);
onChange($(opts.group).find(checkedItems), $(opts.group).find(uncheckedItems));
});
$(opts.row).not(self).on('change', function() {
var unchecked = $(opts.group).find(uncheckedItems).not(self);
$(self).prop('checked', (unchecked.length == 0));
onChange($(opts.group).find(checkedItems).not(self), unchecked);
});
});
};
})(jQuery, window);