Checking whether html/css toggle switch has been set to on/off position in go

252 views
Skip to first unread message

Chris S

unread,
Nov 28, 2016, 1:33:33 AM11/28/16
to golang-nuts
Hi,

I am trying to implement a toggle switch in my webpage. I've followed the site below for doing so:

http://www.w3schools.com/howto/howto_css_switch.asp

I currently have my html file set up with a button and this toggle switch. I also configured my webserver in go to be listening on localhost:8080. And I have a websocket handler configured, so that I can easily pass data through to my webpage on the click of the button.

What I want to do create a toggle switch on my webpage that the user can switch on and off, and then have them click a button. After that button is clicked I want to analyse the users selection using an if condition in my golang code based on whether this toggle switch is on/off, but I cannot figure out how to access this value in go. Any suggestions would be helpful. Also, it'd be ideal to have a toggle switch implemented, but if anyone has any simpler ideas for this use case then I'd be open to them.

Konstantin Khomoutov

unread,
Nov 28, 2016, 3:28:37 AM11/28/16
to Chris S, golang-nuts
On Sun, 27 Nov 2016 22:33:33 -0800 (PST)
Chris S <chris.s...@gmail.com> wrote:

[...]
> I currently have my html file set up with a button and this toggle
> switch. I also configured my webserver in go to be listening on
> localhost:8080. And I have a websocket handler configured, so that I
> can easily pass data through to my webpage on the click of the button.
>
> What I want to do create a toggle switch on my webpage that the user
> can switch on and off, and then have them click a button. After that
> button is clicked I want to analyse the users selection using an if
> condition in my golang code based on whether this toggle switch is
> on/off, but I cannot figure out how to access this value in go. Any
> suggestions would be helpful. Also, it'd be ideal to have a toggle
> switch implemented, but if anyone has any simpler ideas for this use
> case then I'd be open to them.

You're (probably) supposed to use some JavaScript on your page so that
when the user activates your toggle, you perform a request through that
web socket. A handler installed to listen on that websocket in your Go
server should parse the request sent from the browser and act
accordingly.

The best course of action is to break this task into subtasks and have
them solved one-by-one:

* Make sure activating your toggle calls whatever JS code you intend.

For this, use plain console.log("whatever") and see whether it works
in your browser's developer window. Note that contemporary browsers
even allow you to execute your JS handlers step-by-step and set
breakpoints.

* Make sure your JS handler performs the request through the web socket.

Again, this can be checked in the browser's developer window.
It allows you to inspect the request's properties.

* Make sure your Go handler works.

Match that what you install on the Go side with what you've inspected
using the browser's developer tools.

Chris S

unread,
Nov 28, 2016, 3:37:44 AM11/28/16
to Konstantin Khomoutov, golang-nuts
Thanks for the response Konstantin,

I do have my go handler configured, as well as my ws function in javascript configured to be triggered on the click of a button. Forgot to mention this. right now on the click of a button I can execute a terminal command in go, and output the results in realtime on my webpage. But what I want to do is configure a toggle switch on my webpage so that if the switch is in an on position, then when I click the button, I can execute a terminal command in my go code, and if the switch is off, after I click the button my go code will execute a different terminal command. Here is the toggle switch I have implemented: http://www.bootstraptoggle.com/

But now I'm stuck on how to read whether this switch is in its off/on position in my go code. Any suggestions would help.

Konstantin Khomoutov

unread,
Nov 28, 2016, 3:50:48 AM11/28/16
to Chris S, Konstantin Khomoutov, golang-nuts
On Mon, 28 Nov 2016 03:37:32 -0500
Chris S <chris.s...@gmail.com> wrote:

> Thanks for the response Konstantin,
>
> I do have my go handler configured, as well as my ws function in
> javascript configured to be triggered on the click of a button.
> Forgot to mention this. right now on the click of a button I can
> execute a terminal command in go, and output the results in realtime
> on my webpage. But what I want to do is configure a toggle switch on
> my webpage so that if the switch is in an on position, then when I
> click the button, I can execute a terminal command in my go code, and
> if the switch is off, after I click the button my go code will
> execute a different terminal command. Here is the toggle switch I
> have implemented: http://www.bootstraptoggle.com/
>
> But now I'm stuck on how to read whether this switch is in its off/on
> position in my go code. Any suggestions would help.

In general, you can't do this (well, almost [*]).

But you shouldn't -- just reverse the problem: when the user activates
the button to execute the command they have input, read the state of
that toggle and send it along with the command to the server.

The server will then parse out that piece of information from the
webpage's request and switch its course of action accordingly.

In other words, that's no magic -- just try thinking constructively:

1. When the user activates a button on your web page, the server has to
interpret this event *depending* on the state of a toggle control.

2. OK, this means you have to tell the server what's the state of that
toggle control along with the "user activated that button" event.

Konstantin Khomoutov

unread,
Nov 28, 2016, 4:12:37 AM11/28/16
to Chris S, Konstantin Khomoutov, golang-nuts
On Mon, 28 Nov 2016 11:50:26 +0300
Konstantin Khomoutov <flat...@users.sourceforge.net> wrote:

[...]
> > I do have my go handler configured, as well as my ws function in
> > javascript configured to be triggered on the click of a button.
> > Forgot to mention this. right now on the click of a button I can
> > execute a terminal command in go, and output the results in realtime
> > on my webpage. But what I want to do is configure a toggle switch on
> > my webpage so that if the switch is in an on position, then when I
> > click the button, I can execute a terminal command in my go code,
> > and if the switch is off, after I click the button my go code will
> > execute a different terminal command. Here is the toggle switch I
> > have implemented: http://www.bootstraptoggle.com/
> >
> > But now I'm stuck on how to read whether this switch is in its
> > off/on position in my go code. Any suggestions would help.
>
> In general, you can't do this (well, almost [*]).

Forgot to address this point.

Well, with modern browsers and websockets it should be possible to use
the so-called "server sent events" which allows the server to actively
solitict an action from the client.

But the fact this feature exists does not means it should be used in
your case. Your case, in my opinion, is much simpler and I suggest you
to not overengeneer your solution.

[...]
Reply all
Reply to author
Forward
0 new messages