From 9a154f9fac8616975883187f73bf46c2a007122f Mon Sep 17 00:00:00 2001 From: Axel Utech Date: Sun, 10 Jan 2016 20:05:37 +0100 Subject: [PATCH] Fix the click handling for checkboxes inside labels; Move click handling into render function --- js/bootstrap-toggle.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/js/bootstrap-toggle.js b/js/bootstrap-toggle.js index 533914e..288b166 100644 --- a/js/bootstrap-toggle.js +++ b/js/bootstrap-toggle.js @@ -82,6 +82,18 @@ $toggleOn.css('line-height', $toggleOn.height() + 'px') $toggleOff.css('line-height', $toggleOff.height() + 'px') } + + var clickHandler = function(e){ + this.toggle(); + e.preventDefault() + }.bind(this) + + if (this.$toggle.parent().prop('tagName') == 'LABEL') { + this.$toggle.parent().click(clickHandler); + } else { + this.$toggle.click(clickHandler); + } + this.update(true) this.trigger(true) } @@ -171,10 +183,4 @@ $('input[type=checkbox][data-toggle^=toggle]').bootstrapToggle() }) - $(document).on('click.bs.toggle', 'div[data-toggle^=toggle]', function(e) { - var $checkbox = $(this).find('input[type=checkbox]') - $checkbox.bootstrapToggle('toggle') - e.preventDefault() - }) - }(jQuery);