jquery.checkboxes.js 4.2 KB

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