[GopherJS] Register Event Handler (Event Binding)


Two ways to register event handler, use onclick event as example:

Set onclick property

import "github.com/gopherjs/gopherjs/js"

element := js.Global.Get("document").Call("getElementById", "foo")
element.Set("onclick", func(event *js.Object) {
      /* do something to handle click event */
})

addEventListener

import "github.com/gopherjs/gopherjs/js"

element := js.Global.Get("document").Call("getElementById", "foo")
element.Call("addEventListener", "click", func(event *js.Object) {
      /* do something to handle click event */
})

References:

[1]GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, godoc)
[2][Golang] GopherJS DOM Example - Event Binding (addEventListener)