1
+ /*
2
+ *
3
+ * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
4
+ * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
5
+ * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
6
+ *
7
+ * Version 2.1
8
+ * Demo: http://www.texotela.co.uk/code/jquery/checkboxes/
9
+ *
10
+ * $LastChangedDate: 2009-02-07 23:44:21 +0000 (Sat, 07 Feb 2009) $
11
+ * $Rev: 6182 $
12
+ */
13
+
14
+ ; ( function ( $ ) {
15
+
16
+ /*
17
+ * Toggle all checkboxes contained within a form
18
+ *
19
+ * @name toggleCheckboxes
20
+ * @param filter only toggle checkboxes matching this expression
21
+ * @param returnChecked return checkboxes as jQuery object, default false
22
+ * @author Sam Collett (http://www.texotela.co.uk)
23
+ * @example $("#myform").toggleCheckboxes();
24
+ * @example $("#myform").toggleCheckboxes(".onlyme");
25
+ * @example $("#myform").toggleCheckboxes(":not(.notme)");
26
+ * @example $("#myform").toggleCheckboxes("*", true);
27
+ *
28
+ */
29
+ $ . fn . toggleCheckboxes = function ( filter , returnChecked )
30
+ {
31
+ filter = filter || "*" ;
32
+ returnChecked = returnChecked || false ;
33
+ var returnWhat = $ ( [ ] ) ;
34
+ this . each (
35
+ function ( )
36
+ {
37
+ var checked = $ ( "input[type=checkbox]" , this ) . filter ( filter ) . each (
38
+ function ( )
39
+ {
40
+ this . checked = ! this . checked ;
41
+ }
42
+ ) . filter ( ":checked" ) ;
43
+ returnWhat = checked ;
44
+ }
45
+ ) ;
46
+ if ( ! returnChecked )
47
+ {
48
+ returnWhat = this ;
49
+ }
50
+ return returnWhat ;
51
+ } ;
52
+
53
+ /*
54
+ * Check all checkboxes contained within a form
55
+ *
56
+ * @name checkCheckboxes
57
+ * @param filter only check checkboxes matching this expression
58
+ * @param returnChecked return checkboxes as jQuery object, default false
59
+ * @author Sam Collett (http://www.texotela.co.uk)
60
+ * @example $("#myform").checkCheckboxes();
61
+ * @example $("#myform").checkCheckboxes(".onlyme");
62
+ * @example $("#myform").checkCheckboxes(":not(.notme)");
63
+ * @example $("#myform").checkCheckboxes("*", true);
64
+ *
65
+ */
66
+ $ . fn . checkCheckboxes = function ( filter , returnChecked )
67
+ {
68
+ filter = filter || "*" ;
69
+ returnChecked = returnChecked || false ;
70
+ var returnWhat = $ ( [ ] ) ;
71
+ this . each (
72
+ function ( )
73
+ {
74
+ var checked = $ ( "input[type=checkbox]" , this ) . filter ( filter ) . each (
75
+ function ( )
76
+ {
77
+ this . checked = true ;
78
+ }
79
+ ) . filter ( ":checked" ) ;
80
+ returnWhat = checked ;
81
+ }
82
+ ) ;
83
+ if ( ! returnChecked )
84
+ {
85
+ returnWhat = this ;
86
+ }
87
+ return returnWhat ;
88
+ } ;
89
+
90
+ /*
91
+ * UnCheck all checkboxes contained within a form
92
+ *
93
+ * @name unCheckCheckboxes
94
+ * @param filter only check checkboxes matching this expression
95
+ * @param returnUnChecked return unchecked checkboxes as jQuery object, default false
96
+ * @author Sam Collett (http://www.texotela.co.uk)
97
+ * @example $("#myform").unCheckCheckboxes();
98
+ * @example $("#myform").unCheckCheckboxes(".onlyme");
99
+ * @example $("#myform").unCheckCheckboxes(":not(.notme)");
100
+ * @example $("#myform").unCheckCheckboxes("*", true);
101
+ *
102
+ */
103
+ $ . fn . unCheckCheckboxes = function ( filter , returnUnChecked )
104
+ {
105
+ filter = filter || "*" ;
106
+ returnUnChecked = returnUnChecked || false ;
107
+ var returnWhat = $ ( [ ] ) ;
108
+ this . each (
109
+ function ( )
110
+ {
111
+ var unChecked = $ ( "input[type=checkbox]" , this ) . filter ( filter ) . each (
112
+ function ( )
113
+ {
114
+ this . checked = false ;
115
+ }
116
+ ) . filter ( ":not(:checked)" ) ;
117
+ returnWhat = unChecked ;
118
+ }
119
+ ) ;
120
+ if ( ! returnUnChecked )
121
+ {
122
+ returnWhat = this ;
123
+ }
124
+ return returnWhat ;
125
+ } ;
126
+
127
+ /*
128
+ * Makes checkboxes behave like a radio button group
129
+ * i.e. only one can be selected at a time
130
+ *
131
+ * @name radioCheckboxGroup
132
+ * @param name field name (leave blank to apply to all check boxes)
133
+ * @param filter apply to checkboxes matching this expression
134
+ * @author Sam Collett (http://www.texotela.co.uk)
135
+ * @example $.radioCheckboxGroup("fieldname");
136
+ * @example $.radioCheckboxGroup("fieldname", ".myclass");
137
+ * @example $.radioCheckboxGroup("", ".myclass");
138
+ *
139
+ */
140
+ $ . radioCheckboxGroup = function ( name , filter )
141
+ {
142
+ filter = filter || "*" ;
143
+ var expression = "input[type=checkbox]" ;
144
+ if ( name )
145
+ {
146
+ expression += "[name=" + name + "]"
147
+ }
148
+ var x = $ ( expression ) . filter ( filter ) ;
149
+ x . click (
150
+ function ( )
151
+ {
152
+ // uncheck every other box with the same name
153
+ x . not ( this ) . each (
154
+ function ( )
155
+ {
156
+ this . checked = false ;
157
+ }
158
+ ) . end ( ) ;
159
+ }
160
+ ) ;
161
+ } ;
162
+
163
+ } ) ( jQuery ) ;
0 commit comments