jQuery-switchButton icon indicating copy to clipboard operation
jQuery-switchButton copied to clipboard

Implements two public methods: toggle allows toggling the button via js.

Open ErwinM opened this issue 11 years ago • 8 comments

SilentToggle does the same only without triggering a change event. This solves #7.

ErwinM avatar Jan 25 '14 18:01 ErwinM

Added a third 'redraw' method that brings the button back in sync with the checkbox.

ErwinM avatar Jan 26 '14 12:01 ErwinM

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

mikemarty avatar Mar 20 '14 04:03 mikemarty

@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 avatar Mar 20 '14 09:03 olance

@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).

mikemarty avatar Mar 20 '14 23:03 mikemarty

@mikem1977

$("#switch1 input").switchButton("redraw");

You'll have to check out @ErwinM's code though

olance avatar Mar 21 '14 09:03 olance

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.

mikemarty avatar Mar 22 '14 03:03 mikemarty

@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 avatar Mar 23 '14 19:03 ErwinM

@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.

mikemarty avatar Mar 24 '14 22:03 mikemarty