YourFavoriteIntern
YourFavoriteIntern

Reputation: 47

bootstrapSwitch Don't toggle switch on click

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);
       }
    }
})

Upvotes: 0

Views: 500

Answers (1)

Don't Panic
Don't Panic

Reputation: 14520

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;
    }
});

Upvotes: 1

Related Questions