I've created an jQuery Mobile flipswitch like this
<select id="flip" data-role="flipswitch">
<option value="animal">Lion</option>
<option value="flower">Rose</option>
</select>
and I listen to changes on this flipswitch
$( document ).on( 'change', '#flip', function( e ) {
console.log( 'changed' );
}
But I don't want the event to be triggered if I change the value of the flipswitch by
$( '#flip' ).val( 'flower' ).flipswitch( 'refresh' );
How do I check if a flipswitch has changed by interacting with it directly or setting a value and refreshing the flipswitch?
I've created an example of the unwanted behavior under this JSFiddle.
You need to use
.on()
to attachchange
event and.off()
to remove it - before you bind it again - whenever you change the value dynamically.Wrap all your code in
pagecreate
event, it is equvilant to.ready()
.