-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tabcheckbox.js
75 lines (58 loc) · 1.93 KB
/
jquery.tabcheckbox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Jquery Plugin Tabbed Combo Box
// A plugin to render checbox as tabbed button
// Version 0.1 - 24 Nov 2014
// by Peter Boccia
// peterboccia88@gmail.com
(function ($) {
$.tabCheckBox = function (element, options) {
var defaults = {
activeClass: 'active',
inactiveClass: 'inactive',
attributeLabel: 'data-text',
onChange: function () { }
}
var plugin = this;
plugin.settings = {}
var $element = $(element);
var span;
plugin.init = function () {
plugin.settings = $.extend({}, defaults, options);
$element.hide();
span = $('<span />')
.addClass('tabbedCheckbox')
.text($element.attr(plugin.settings.attributeLabel));
if ($element.is(':checked') || $element.val() == 'true') {
plugin.select();
}
else {
plugin.deselect();
}
span.click(function (e) {
if ($element.val() == 'true') {
plugin.deselect();
}
else {
plugin.select();
}
});
span.insertAfter($element);
}
plugin.select = function () {
span.removeClass(plugin.settings.inactiveClass).addClass(plugin.settings.activeClass);
$element.val(true);
}
plugin.deselect = function () {
span.removeClass(plugin.settings.activeClass).addClass(plugin.settings.inactiveClass);
$element.val(false);
}
plugin.init();
}
$.fn.tabCheckBox = function (options) {
return this.each(function () {
if (undefined == $(this).data('tabCheckBox')) {
var plugin = new $.tabCheckBox(this, options);
$(this).data('tabCheckBox', plugin);
}
});
}
})(jQuery);