├── README.markdown └── jquery.checkall.js /README.markdown: -------------------------------------------------------------------------------- 1 | # jQuery CheckAll Plugin # 2 | 3 | ## It's really simple. ## 4 | 5 | Basic demo: http://jsfiddle.net/mattball/NBrg8/ 6 | 7 | ### Using the plugin ### 8 | 9 | To use the checkbox with id `master` to control the checkboxes that are children of the form with id `myForm`, this is all you need: 10 | 11 | ``` 12 | $('#master').checkAll('#myForm input:checkbox'); 13 | ``` 14 | 15 | To override any of the default settings, pass in an `options` object as the second argument. For instance, if I don't want to update the master checkbox when a slave is changed: 16 | 17 | ``` 18 | $('#master').checkAll('#myForm input:checkbox', {sync: false}); 19 | ``` 20 | 21 | ### Options ### 22 | 23 | All are optional (pun intended). 24 | 25 | ``` 26 | key | default | description 27 | --------------------------------------------------------------------------------------- 28 | sync | true | boolean, keep the master sync'd with the slaves when a slave changes 29 | onClick | null | callback function, called whenever a slave checkbox is clicked 30 | onMasterClick | null | callback function, called when the master checkbox is clicked 31 | reportTo | null | callback function, receives the number of slaves currently checked 32 | ``` 33 | 34 | ### Bonus awesomeness ### 35 | 36 | - [It doesn't break if you accidentally select the master checkbox along with the slaves.](http://jsfiddle.net/mattball/fCEPa/) 37 | - It's lightweight thanks to `.live()`, so it can handle [as many checkboxes as you want to throw at it](http://jsfiddle.net/mattball/ZBjUV/). 38 | - Compatibility tested with jQuery 1.4.2 through 1.9. 39 | 40 | ### Questions? ### 41 | 42 | Please ask for help on [Stack Overflow](http://stackoverflow.com)! I prefer to answer in a public manner in case other devs have the same question. 43 | -------------------------------------------------------------------------------- /jquery.checkall.js: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------- 2 | CheckAll plugin for jQuery 3 | Version: 1.4 4 | 5 | Copyright (c) 2012 Matt Ball 6 | 7 | April 2, 2012 8 | 9 | Requires: jQuery 1.4.2+ 10 | Last tested with: 1.4.4, 1.5.2, 1.6, 1.7.1, 1.8.2, 1.9 11 | ------------------------------------------------------*/ 12 | 13 | ;(function($) 14 | { 15 | $.fn.checkAll = function (group, options) 16 | { 17 | var opts = $.extend({}, $.fn.checkAll.defaults, options), 18 | $master = this, 19 | 20 | $slaves = $(group), 21 | selector, 22 | groupSize, 23 | onClick = typeof opts.onClick === 'function' ? opts.onClick : null, 24 | onMasterClick = typeof opts.onMasterClick === 'function' ? opts.onMasterClick : null, 25 | reportTo = typeof opts.reportTo === 'function' ? $.proxy(opts.reportTo, $master) : null, 26 | 27 | // for compatibility with 1.4.2 through 1.6 28 | propFn = typeof $.fn.prop === 'function' ? 'prop' : 'attr', 29 | onFn = typeof $.fn.on === 'function' ? 'on' : 'live', 30 | offFn = typeof $.fn.off === 'function' ? 'off' : 'die'; 31 | 32 | // omit the master if it was accidentally selected with the slaves 33 | if ($slaves.index($master) === -1) 34 | { 35 | selector = $slaves.selector; 36 | } 37 | else 38 | { 39 | $slaves = $slaves.not($master.selector); 40 | selector = $slaves.selector.replace('.not(', ':not('); 41 | } 42 | 43 | groupSize = $slaves.length; 44 | 45 | if (groupSize === 0) 46 | { 47 | // this is kind of a problem 48 | groupSize = -1; 49 | } 50 | 51 | function _countChecked() 52 | { 53 | return $slaves.filter(':checked').length; 54 | } 55 | 56 | function _autoEnable() 57 | { 58 | var numChecked = _countChecked(); 59 | $master[propFn]('checked', groupSize === numChecked); 60 | if (reportTo) 61 | { 62 | reportTo(numChecked); 63 | } 64 | } 65 | 66 | function _autoDisable() 67 | { 68 | $master[propFn]('checked', false); 69 | if (reportTo) 70 | { 71 | reportTo(_countChecked()); 72 | } 73 | } 74 | 75 | $master.unbind('click.checkAll').bind('click.checkAll', function (e) 76 | { 77 | var check_val = e.target.checked; 78 | $slaves.add($master)[propFn]('checked', check_val); 79 | 80 | if (onMasterClick) 81 | { 82 | onMasterClick.apply(this); 83 | } 84 | 85 | if (reportTo) 86 | { 87 | reportTo(check_val ? _countChecked() : 0); 88 | } 89 | }); 90 | 91 | 92 | if (opts.sync) 93 | { 94 | $(selector)[offFn]('click.checkAll')[onFn]('click.checkAll', function () 95 | { 96 | this.checked ? _autoEnable() : _autoDisable(); 97 | 98 | if (onClick) 99 | { 100 | onClick.apply(this); 101 | } 102 | }); 103 | } 104 | 105 | _autoEnable(); 106 | 107 | return this; 108 | }; 109 | 110 | $.fn.checkAll.defaults = {sync: true}; 111 | }(jQuery)); 112 | --------------------------------------------------------------------------------