aboutsummaryrefslogtreecommitdiff
path: root/js/src/button.js
diff options
context:
space:
mode:
authorLaussel Loïc <[email protected]>2020-03-16 13:28:33 +0100
committerGitHub <[email protected]>2020-03-16 14:28:33 +0200
commit8b6dd449d7553410dce7ecd2e78580928a6c082f (patch)
tree46029c1c004a90be5ea6b111d0c951e40ddf97fc /js/src/button.js
parentac5685d368298d56bbdf561f4ddf65563d8bffc1 (diff)
downloadbootstrap-8b6dd449d7553410dce7ecd2e78580928a6c082f.tar.xz
bootstrap-8b6dd449d7553410dce7ecd2e78580928a6c082f.zip
fix `$().button('toggle')` not working for checkbox inside label (#30388)
Diffstat (limited to 'js/src/button.js')
-rw-r--r--js/src/button.js16
1 files changed, 8 insertions, 8 deletions
diff --git a/js/src/button.js b/js/src/button.js
index f5b4fa0a7..76ea9e337 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')
}
})