From: Laussel Loïc Date: Mon, 16 Mar 2020 12:28:33 +0000 (+0100) Subject: fix `$().button('toggle')` not working for checkbox inside label (#30388) X-Git-Tag: v4.5.0~98 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8b6dd449d7553410dce7ecd2e78580928a6c082f;p=thirdparty%2Fbootstrap.git fix `$().button('toggle')` not working for checkbox inside label (#30388) --- diff --git a/js/src/button.js b/js/src/button.js index f5b4fa0a70..76ea9e3377 100644 --- a/js/src/button.js +++ b/js/src/button.js @@ -84,17 +84,13 @@ class Button { $(activeElement).removeClass(ClassName.ACTIVE) } } - } else if (input.type === 'checkbox') { - if (this._element.tagName === 'LABEL' && input.checked === this._element.classList.contains(ClassName.ACTIVE)) { - triggerChangeEvent = false - } - } else { - // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input - triggerChangeEvent = false } if (triggerChangeEvent) { - input.checked = !this._element.classList.contains(ClassName.ACTIVE) + // if it's not a radio button or checkbox don't add a pointless/invalid checked property to the input + if (input.type === 'checkbox' || input.type === 'radio') { + input.checked = !this._element.classList.contains(ClassName.ACTIVE) + } $(input).trigger('change') } @@ -147,6 +143,7 @@ class Button { $(document) .on(Event.CLICK_DATA_API, Selector.DATA_TOGGLE_CARROT, (event) => { let button = event.target + const initialButton = button if (!$(button).hasClass(ClassName.BUTTON)) { button = $(button).closest(Selector.BUTTON)[0] @@ -162,6 +159,9 @@ $(document) return } + if (initialButton.tagName === 'LABEL' && inputBtn && inputBtn.type === 'checkbox') { + event.preventDefault() // work around event sent to label and input + } Button._jQueryInterface.call($(button), 'toggle') } }) diff --git a/js/tests/unit/button.js b/js/tests/unit/button.js index e5b349363b..e92d229e6d 100644 --- a/js/tests/unit/button.js +++ b/js/tests/unit/button.js @@ -181,7 +181,7 @@ $(function () { }) QUnit.test('should check for closest matching toggle', function (assert) { - assert.expect(12) + assert.expect(18) var groupHTML = '
' + '