drinu16 drinu16 - 1 year ago 63
jQuery Question

Bootstrap switchChange event still firing when changing state

I have this switch below:

<input id="upperLightSwitch" type="checkbox" checked data-size="mini">

With this, switchChange (on change) event in the
triggers fine when the switch state is changed by clicking on it.

$('#upperLightSwitch').on('switchChange.bootstrapSwitch', function (event, state) {

My problem is when I check a value from the database and it is 0. I am setting the toggle switch state to false and setting the 3rd parameter to false which says to not fire the event on change, but the event still triggers as both methods in the event above are being executed.

Quote from Bootsrap website:

$('#toggle-state-switch').bootstrapSwitch('state', false, false); // Set the state as off and do not trigger switchChange event

The one I am using

$('#upperLightSwitch').bootstrapSwitch('state', false,false );

Before the code of the event, I am calling a method to check that value with the database when everything loads.

What am I missing?

EDIT: Bootstrap switch link

Answer Source

The last parameter should be true not false, like below to disable the event from triggering

$('#upperLightSwitch').bootstrapSwitch('state', false,true );