[GopherJS] Focus and Blur of DOM Element


Focus and Blur of DOM element via GopherJS.

index.html | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>GopherJS Focus/Blur of DOM Element Demo</title>
</head>
<body>

<input id="i1" type="text" value="" autofocus>
<br>
<input id="i2" type="text" value="">
<br>
<button id="btn" type="button">click me to change focus!</button>

<script src="app.js"></script>
</body>
</html>
app.go | repository | view raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package main

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

func main() {
	i1 := js.Global.Get("document").Call("getElementById", "i1")
	i2 := js.Global.Get("document").Call("getElementById", "i2")
	btn := js.Global.Get("document").Call("getElementById", "btn")

	btn.Set("onclick", func(e *js.Object) {
		i1.Call("blur")
		i2.Call("focus")
	})
}

Tested on:

  • Ubuntu Linux 16.04
  • Go 1.7.4
  • Chromium Version 53.0.2785.143 Built on Ubuntu , running on Ubuntu 16.04 (64-bit)

References:

[1]GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, godoc)
[2]