Skip to content

Instantly share code, notes, and snippets.

@jarrettmeyer
Forked from anonymous/MultiSelect.js
Last active December 28, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarrettmeyer/7572289 to your computer and use it in GitHub Desktop.
Save jarrettmeyer/7572289 to your computer and use it in GitHub Desktop.
// We are creating a new class definition. The class definition itself
// will be added to the global window object.
window.MultiSelect = (function() {
function MultiSelect(options) {
var defaults = {
checkboxSelector: ".multi-select-checkbox",
onSelector: ".multi-select-on",
selectAllSelector: "#multi-select-all"
};
// Using jquery's extend to map user options onto a
// pre-defined object.
this.options = $.extend(defaults, options);
}
MultiSelect.prototype.hide = function () {
console.log("MultiSelect: hide");
$(this.options.onSelector).hide();
};
MultiSelect.prototype.initialize = function() {
this.hide();
this.listenForCheckedEvent();
};
MultiSelect.prototype.listenForCheckedEvent = function() {
// Be sure to call .bind() on the callback, otherwise the callback
// will have the context of the checkbox, not the class definition.
$(this.options.selectAllSelector).click(function() {
var isChecked = $(this.options.selectAllSelector).is(":checked");
this.toggleChecked(isChecked);
}.bind(this));
};
MultiSelect.prototype.setChecked = function (value) {
console.log("MultiSelect: setChecked(" + value + ")");
$(this.options.checkboxSelector).prop("checked", value);
};
MultiSelect.prototype.show = function () {
console.log("MultiSelect: show");
$(this.options.onSelector).show();
};
MultiSelect.prototype.toggleChecked = function(isChecked) {
if (isChecked) {
this.setChecked(true);
} else {
this.setChecked(false);
}
};
return MultiSelect;
})();
// Usage...
// var multiSelect = new MultiSelect();
// multiSelect.initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment