Skip to content

Instantly share code, notes, and snippets.

Created June 7, 2012 01:43
Show Gist options
  • Save anonymous/2886026 to your computer and use it in GitHub Desktop.
Save anonymous/2886026 to your computer and use it in GitHub Desktop.
Simple Tristate Checkbox
/*!
* Edited By Daniel Teixeira
*
* Tri-State checkbox
*
* Copyright 2011, http://webworld-develop.blogspot.com/
* Artistic License 2.0
* http://www.opensource.org/licenses/artistic-license-2.0
*
* Date: Fr Apr 22 08:00:00 2011 -0200
**/
(function ($) {
var settings = { classes: { checkbox: "customcheck", checked: "customcheckfull", partial: "customcheckpartial", unchecked: "customchecknone"} };
var methods =
{
init: function (options) {
return this.each(function () {
if (options) { $.extend(settings, options); }
var main = $(this).hide();
var ch = settings.classes.checked;
var part = settings.classes.partial;
var unch = settings.classes.unchecked;
var start = unch;
if (main.is(":checked")) {
start = ch;
}
var $this = $("<span class='" + settings.classes.checkbox + " " + start + "'></span>").insertBefore(main);
$this.click(function () {
if ($this.hasClass(ch)) {
$this.removeClass(ch).addClass(part);
main.attr("checked", "maybe");
}
else if ($this.hasClass(part)) {
$this.removeClass(part).addClass(unch);
main.attr("checked", false);
}
else if ($this.hasClass(unch)) {
$this.removeClass(unch).addClass(ch);
main.attr("checked", false);
}
});
});
}
};
$.fn.tristate = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment