Skip to content

Commit c45d07f

Browse files
committed
Support autofocus attribute.
1 parent d596626 commit c45d07f

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

dist/bootstrap-checkbox.js

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,22 @@ if (typeof jQuery === 'undefined') {
1515

1616
var Checkboxpicker = function(element, options) {
1717
this.element = element;
18-
this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, $(element).data());
18+
this.$element = $(element).hide();
19+
20+
this.options = $.extend({}, $.fn.checkboxpicker.defaults, options, this.$element.data());
1921

2022
// .btn-group-justified only works with <a> elements as the <button> doesn't pick up the styles
2123
this.$buttons = $('<a><a>').addClass('btn').click(this.clicked.bind(this));
2224

2325
this.$off = this.$buttons.eq(0).html(this.options.offLabel);
2426
this.$on = this.$buttons.eq(1).html(this.options.onLabel);
2527

26-
this.init();
27-
28-
$(element).hide().change(this.render.bind(this));
29-
3028
this.$group = $('<div class="btn-group">')
3129
.append(this.$buttons)
3230
.keydown(this.keydown.bind(this))
3331
.insertAfter(element);
3432

35-
if (this.options.style) {
36-
this.$group.addClass(this.options.style);
37-
}
38-
39-
if (element.title) {
40-
this.$group.attr('title', element.title);
41-
}
42-
else {
43-
if (this.options.offTitle) {
44-
this.$off.attr('title', this.options.offTitle);
45-
}
46-
47-
if (this.options.onTitle) {
48-
this.$on.attr('title', this.options.onTitle);
49-
}
50-
}
51-
52-
if (element.disabled) {
53-
this.$buttons.addClass('disabled');
54-
}
55-
else {
56-
this.$group.attr('tabindex', 0);
57-
}
33+
this.init();
5834
};
5935

6036
Checkboxpicker.prototype = {
@@ -67,6 +43,36 @@ if (typeof jQuery === 'undefined') {
6743
}
6844

6945
this.$buttons.not('.active').addClass(this.options.defaultClass);
46+
47+
if (this.options.style) {
48+
this.$group.addClass(this.options.style);
49+
}
50+
51+
if (this.element.title) {
52+
this.$group.attr('title', this.element.title);
53+
}
54+
else {
55+
if (this.options.offTitle) {
56+
this.$off.attr('title', this.options.offTitle);
57+
}
58+
59+
if (this.options.onTitle) {
60+
this.$on.attr('title', this.options.onTitle);
61+
}
62+
}
63+
64+
if (this.element.disabled) {
65+
this.$buttons.addClass('disabled');
66+
}
67+
else {
68+
this.$element.change(this.render.bind(this));
69+
70+
this.$group.attr('tabindex', 0);
71+
72+
if (this.element.autofocus) {
73+
this.$group.focus();
74+
}
75+
}
7076
},
7177
render: function() {
7278
this.$group.not(':focus').focus();
@@ -75,7 +81,7 @@ if (typeof jQuery === 'undefined') {
7581
this.$on.toggleClass(this.options.onClass);
7682
},
7783
change: function() {
78-
$(this.element).prop('checked', !this.element.checked).change();
84+
this.$element.prop('checked', !this.element.checked).change();
7985
},
8086
clicked: function(event) {
8187
if (!$(event.target).hasClass('active')) {

dist/bootstrap-checkbox.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)