Home > database >  bootstrapSwitch Don't toggle switch on click
bootstrapSwitch Don't toggle switch on click

Time:10-25

I have a bootstrap switch, but I don't want it to toggle everytime. I want to check that a condition is true, and only then toggle the switch. How can I do that? This is what I have so far:

$("[name='relVal']").bootstrapSwitch({
    onSwitchChange: function(event, state) {
       if (condition_is_met) {
          $("[name='relVal']").bootstrapSwitch('state', true);
       }
    }
})

CodePudding user response:

From the docs:

onSwitchChange

Callback function to execute on switch state change. If false is returned, the status will be reverted, otherwise nothing changes

So all you need to do is return true/false for the switch to actually happen or not.

$("[name='relVal']").bootstrapSwitch({
    onSwitchChange: function(event, state) {
        if (condition_is_met) {
            return true;
        }

        return false;

        // Or as a one-liner
        // return condition_is_met;
    }
});
  • Related