Implements two public methods: toggle allows toggling the button via js.
SilentToggle does the same only without triggering a change event. This solves #7.
Added a third 'redraw' method that brings the button back in sync with the checkbox.
@ErwinM, I'm a total newbie. Can you tell me how to trigger the 'redraw' method? What do I call after this?
$("#switch1 input").prop('checked', true);
@ErwinM thanks for your PR, however I wonder if this is really the way to go?
Shouldn't we listen to the change event of the underlying checkbox and silently update the switch button when the checkbox state changes? We should code something against entering an infinite loop of events though, but that doesn't seem unfeasible.
@olance, do you know how to trigger the redraw function that @ErwinM added? I'm using this for my kid's science fair project that is coming up! (smartphone-controlled home automation).
@mikem1977
$("#switch1 input").switchButton("redraw");
You'll have to check out @ErwinM's code though
Yup, I've tried that:
$("#switch1 input").switchButton("redraw");
I keep getting:
Uncaught TypeError: Object [object Object] has no method 'switchButton'
The selector is definitely returning something, and I do this to turn the checkbox into the switchButton (which works fine):
$("#switch1 input").switchButton({
show_labels: true,
width: 100,
height: 80,
button_width: 50
});
One of these days I'll figure out this whole jQuery/Javascript world. Cool widget though.
@mikem1977 Are you sure you checked out my fork of SwitchButton from my Github repo?
@olance I agree what you propose is nicer. However, this suited my needs and I thought perhaps it would help other. There is no harm in having a redraw method? Up to you of course.
@ErwinM, I got it to work.
I had to do something like:
""" var $myswitch;
function init_switches() { $myswitch = $("#switch1 input").switchButton({ show_labels: true, width: 100, height: 80, button_width: 50 }); }
function update_switch_state() { $myswitch.switchButton("redraw"); // $("#switch1 input").switchButton("redraw") does NOT work }
"""
I'm quite the JQuery/Javascript newbie, so I can't really understand/explain why using a variable works instead of the selector.