I was able to add a button with n onclick action as below:
var f js.Func
f = js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer f.Release()
Window.Call("alert", "Hello World")
// or
// alert := Window.Get("alert")
// alert.Invoke("Hello World")
return nil
})
// f = function(){ alert("Hello World") };
var Window = js.Global()
// document = window.document
document := Window.Get("document")
// btn = document.createElement("button")
btn := document.Call("createElement", "button")
// btn.innerText = "Click me"
btn.Set("innerText", "Click me")
// btn.style.background='#000000';
btn.Get("style").Call("setProperty", "background-color", "blue")
// btn.onclick = f
btn.Set("onclick", f)
// document.body.appendChild(btn)
document.Get("body").Call("appendChild", btn)
It worked perfectly, but I tried replicating JS code:
btn.addEventListener("click",function(event){ f });
As:
btn.Call("addEventListener", "click", f )
// or
btn.Call("addEventListener").Set("click", f)
But none of them worked, any idea why?