forked from tomekwojcik/jQuery-Custom-Checkboxes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery-checkbox-2.0.js
More file actions
70 lines (59 loc) · 2.35 KB
/
jquery-checkbox-2.0.js
File metadata and controls
70 lines (59 loc) · 2.35 KB
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
/**
* jQuery custom checkboxes
*
* Copyright (c) 2010-2012 Tomasz Wójcik (bthlabs.pl)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
* @version 2.0
* @category visual
* @package jquery
* @subpakage ui.checkbox
* @author Tomasz Wójcik <labs@tomekwojcik.pl>
* @contributor Nicolas Turlais
*/
(function() {
jQuery.fn.checkbox = function(options) {
options = options || {};
var defaults = {
'className': 'jquery-checkbox',
'checkedClass': 'jquery-checkbox-on'
};
var settings = jQuery.extend(defaults, options);
return this.each(function() {
var self = jQuery(this);
var replacement = jQuery(
'<div class="' + settings.className + '-wrapper">' +
'<a class="' + settings.className + '" href="#" name="' + self.attr('id') + '"></a>' +
'</div>'
);
var element = jQuery('a', replacement);
if (self.prop('checked')) {
element.addClass(settings.checkedClass);
}
element.on('click', function(event) {
event.preventDefault();
event.stopPropagation();
var input = jQuery('input#' + jQuery(this).attr('name'), replacement.parent());
if (input.prop('checked')) {
input.removeAttr('checked');
} else {
input.prop('checked', true);
}
input.trigger('change');
return false;
});
self.on('change', function(event) {
var input = jQuery(this);
if (input.prop('checked')) {
jQuery('a[name=' + input.attr('id') + ']', replacement.parent()).addClass(settings.checkedClass);
} else {
jQuery('a[name=' + input.attr('id') + ']', replacement.parent()).removeClass(settings.checkedClass);
}
return true;
});
self.css({ 'position': 'absolute', 'left': '-200px'}).before(replacement);
replacement.parent().css('overflow', 'hidden');
});
}
})();