[GopherJS] createElement and createTextNode DOM Example


DOM Example of createElement and createTextNode via GopherJS.

The following code creates DOM equivalent to <div><span>Hello</span> World!</div>.

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

func createElementAndTextNode() *js.Object {
      div := js.Global.Get("document").Call("createElement", "div")

      span := js.Global.Get("document").Call("createElement", "span")
      span.Set("textContent", "Hello")
      div.Call("appendChild", span)

      text := js.Global.Get("document").Call("createTextNode", " World!")
      div.Call("appendChild", text)

      return div
}

Tested on:

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

References:

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