[Golang] querySelectorAll and querySelector Example by GopherJS


Golang querySelectorAll and querySelector Example by GopherJS.

querySelector

The querySelector() method returns a DOM element object:

// document object
d := js.Global.Get("document")
// return element whose css class is setting-menu
element := d.Call("querySelector", ".setting-menu")
// do something with the element ...

querySelectorAll

The querySelectorAll() method returns a NodeList:

// document object
d := js.Global.Get("document")
// return a nodelist in which the elements whose css class are about-link
nodeList := d.Call("querySelectorAll", ".about-link")
// access individual element
length := nodeList.Get("length").Int()
for i := 0; i < length; i++ {
        // get i-th element in nodelist
        element := nodeList.Call("item", i)
        // do something with the element ...
}

Tested on: Ubuntu Linux 15.10, Go 1.5.3.


References:

[1]GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, godoc)
[2]Bindings · gopherjs/gopherjs Wiki · GitHub