Skip to content
This repository was archived by the owner on May 28, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ Call the `checkAll()` function on your "check all" checkbox to initialize the pl
<script>
$(document).ready(function() {
$('#check-all').checkAll();
//here you can get what you've checked id,id,id,...
var ids = $('#check-all').data('plugin_checkAll').values();
});
</script>
```
Expand Down
108 changes: 58 additions & 50 deletions jquery-check-all.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,61 @@
;(function ($, window, document) {
var pluginName = 'checkAll';

var defaults = {
container: document,
childCheckboxes: 'input[type=checkbox]',
showIndeterminate: false
};

function checkAll(element, options) {
this.$el = $(element);
this.options = $.extend({}, defaults, this.$el.data(), options) ;
this.init();
}

checkAll.prototype.init = function() {
this._checkChildren();

var plugin = this;

this.$el.on('change', function(e) {
var $children = $(plugin.options.childCheckboxes, plugin.options.container).not(plugin.$el);
$children.prop('checked', $(this).prop('checked'));
});

$(this.options.container).on('change', plugin.options.childCheckboxes, function(e) {
plugin._checkChildren();
});
};

// prevent multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new checkAll(this, options));
}
});
}

checkAll.prototype._checkChildren = function() {
var totalCount = $(this.options.childCheckboxes, this.options.container).not(this.$el).length;
var checkedCount = $(this.options.childCheckboxes,this.options.container)
.filter(':checked').not(this.$el).length;

var indeterminate = this.options.showIndeterminate &&
checkedCount > 0 && checkedCount < totalCount;

this.$el.prop('indeterminate', indeterminate);
this.$el.prop('checked', checkedCount === totalCount && totalCount !== 0);
}
var pluginName = 'checkAll';

var defaults = {
container: document,
childCheckboxes: 'input[type=checkbox]',
showIndeterminate: false
};

function CheckAll(element, options) {
this.$el = $(element);
this.options = $.extend({}, defaults, this.$el.data(), options) ;
this.init();
}

CheckAll.prototype.init = function() {
this._checkChildren();

var plugin = this;

this.$el.on('change', function(e) {
var $children = $(plugin.options.childCheckboxes, plugin.options.container).not(plugin.$el);
$children.prop('checked', $(this).prop('checked'));
});

$(this.options.container).on('change', plugin.options.childCheckboxes, function(e) {
plugin._checkChildren();
});
};

// prevent multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new CheckAll(this, options));
}
});
};

CheckAll.prototype._checkChildren = function() {
var totalCount = $(this.options.childCheckboxes, this.options.container).not(this.$el).length;
var checkedCount = $(this.options.childCheckboxes,this.options.container)
.filter(':checked').not(this.$el).length;

var indeterminate = this.options.showIndeterminate &&
checkedCount > 0 && checkedCount < totalCount;

this.$el.prop('indeterminate', indeterminate);
this.$el.prop('checked', checkedCount === totalCount && totalCount !== 0);
};

CheckAll.prototype.values = function () {
var ids = '';
$(this.options.childCheckboxes, this.options.container).filter(':checked').not(this.$el).each(function (i, v) {
ids += $(v).val() + ',';
});
return ids.slice(0, -1);
};

})(jQuery, window, document);