bootstrap-switch
bootstrap-switch copied to clipboard
onSwitchChange not firing if indeterminate option is set
To reproduce:
- $("[name='my-checkbox']").bootstrapSwitch({ 'indeterminate': true });
- Click the switch to toggle from indeterminate to Off
- Bug: expected onSwitchChange to fire with state = false
- Note: Toggling the switch from indeterminate to On works as expected; ie: onSwitchChange fires with state = true. The root issue is that this.options.state is initialized to false when this.options.indeterminate = true, so switching the switch to the Off state already has options.state = false, and thus no onSwitchChange event is fired.
To fix in bootstrap-switch.js version 3.3.2, add these lines:
handleWidth: this.$element.data("handle-width"),
labelWidth: this.$element.data("label-width"),
baseClass: this.$element.data("base-class"),
wrapperClass: this.$element.data("wrapper-class")
}, options);
+ if (this.options.indeterminate) {
+ this.options.state = undefined;
+ }
Thanks that help me :D
Alternative temporary fix approach during intialisation, if the bootstrap-switch.js file is treated as an asset that should not be modified:
$(node).find("input.bootstrap-switch").each(function () {
var input = $(this);
input.bootstrapSwitch();
if (input.prop("indeterminate")) {
// Temporary fix for state change detection not working if switch is changed from indeterminate to
// off, cf. https://github.com/Bttstrp/bootstrap-switch/issues/541
var data = input.data("bootstrap-switch");
if (data) {
data.options.state = undefined;
}
}
});
Works fine with the v4 branch (last commit at the time of this comment was 88cdb642b19cab292fc2977f79a92346e852616c).