-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.checkboxes.js
More file actions
165 lines (159 loc) · 3.92 KB
/
jquery.checkboxes.js
File metadata and controls
165 lines (159 loc) · 3.92 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
*
* Copyright (c) 2006-2016 Sam Collett
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
* Version 1.0
*/
(function(factory){
if(typeof define === 'function' && define.amd){
define(['jquery'], factory);
}else{
factory(window.jQuery);
}
}(function($) {
/*
* Toggle all checkboxes contained within a form
*
* @name toggleCheckboxes
* @param filter only toggle checkboxes matching this expression
* @param returnChecked return checkboxes as jQuery object, default false
* @author Sam Collett
* @example $("#myform").toggleCheckboxes();
* @example $("#myform").toggleCheckboxes(".onlyme");
* @example $("#myform").toggleCheckboxes(":not(.notme)");
* @example $("#myform").toggleCheckboxes("*", true);
*
*/
$.fn.toggleCheckboxes = function(filter, returnChecked)
{
filter = filter || "*";
returnChecked = returnChecked || false;
var returnWhat = $([]);
this.each(
function()
{
var checked = $("input[type=checkbox]", this).filter(filter).each(
function()
{
this.checked = !this.checked;
}
).filter(":checked");
returnWhat = checked;
}
);
if(!returnChecked)
{
returnWhat = this;
}
return returnWhat;
};
/*
* Check all checkboxes contained within a form
*
* @name checkCheckboxes
* @param filter only check checkboxes matching this expression
* @param returnChecked return checkboxes as jQuery object, default false
* @author Sam Collett
* @example $("#myform").checkCheckboxes();
* @example $("#myform").checkCheckboxes(".onlyme");
* @example $("#myform").checkCheckboxes(":not(.notme)");
* @example $("#myform").checkCheckboxes("*", true);
*
*/
$.fn.checkCheckboxes = function(filter, returnChecked)
{
filter = filter || "*";
returnChecked = returnChecked || false;
var returnWhat = $([]);
this.each(
function()
{
var checked = $("input[type=checkbox]", this).filter(filter).each(
function()
{
this.checked = true;
}
).filter(":checked");
returnWhat = checked;
}
);
if(!returnChecked)
{
returnWhat = this;
}
return returnWhat;
};
/*
* UnCheck all checkboxes contained within a form
*
* @name unCheckCheckboxes
* @param filter only check checkboxes matching this expression
* @param returnUnChecked return unchecked checkboxes as jQuery object, default false
* @author Sam Collett
* @example $("#myform").unCheckCheckboxes();
* @example $("#myform").unCheckCheckboxes(".onlyme");
* @example $("#myform").unCheckCheckboxes(":not(.notme)");
* @example $("#myform").unCheckCheckboxes("*", true);
*
*/
$.fn.unCheckCheckboxes = function(filter, returnUnChecked)
{
filter = filter || "*";
returnUnChecked = returnUnChecked || false;
var returnWhat = $([]);
this.each(
function()
{
var unChecked = $("input[type=checkbox]", this).filter(filter).each(
function()
{
this.checked = false;
}
).filter(":not(:checked)");
returnWhat = unChecked;
}
);
if(!returnUnChecked)
{
returnWhat = this;
}
return returnWhat;
};
/*
* Makes checkboxes behave like a radio button group
* i.e. only one can be selected at a time
*
* @name radioCheckboxGroup
* @param name field name (leave blank to apply to all check boxes)
* @param filter apply to checkboxes matching this expression
* @author Sam Collett
* @example $.radioCheckboxGroup("fieldname");
* @example $.radioCheckboxGroup("fieldname", ".myclass");
* @example $.radioCheckboxGroup("", ".myclass");
*
*/
$.radioCheckboxGroup = function(name, filter)
{
filter = filter || "*";
var expression = "input[type=checkbox]";
if(name)
{
expression += "[name=" + name + "]"
}
var x = $(expression).filter(filter);
x.click(
function()
{
// uncheck every other box with the same name
x.not(this).each(
function()
{
this.checked = false;
}
).end();
}
);
};
})(jQuery);