Skip to content

JS Values

maxence-charriere edited this page Jun 28, 2020 · 6 revisions

Table of Contents

Description

In some scenarios, interacting with javascript values can be a necessity. A good example is getting the value of an HTML input when the change event is fired.

For compatibility and tooling reasons, the go-app package is wrapping the syscall/js package.

This article contains examples of common operation involving javascript values.

Prevent default

An example that prevents the context menu to pop when the user is right-clicking on a div:

type foo struct {
    app.Compo
}

func (f *foo) Render() app.UI {
    return app.Div().
        OnChange(f.onContextMenu).
        Body(
            app.Text("Don't copy me!"),
        )
}

func (f *foo) onContextMenu(ctx app.Context, e app.Event) {
	e.PreventDefault()
}

Get input value

An example that shows how to get an input value after being changed by user interaction:

type foo struct {
    app.Compo
}

func (f *foo) Render() app.UI {
    return app.Input().OnChange(f.onInputChange)
}

func (f *foo) onInputChange(ctx app.Context, e app.Event) {
    v := ctx.JSSrc().Get("value").String()
    fmt.Println("input value:", v)
}
Clone this wiki locally