Make DOM manipulation in Go as similar to JavaScript as possible. (via GopherJS or WebAssembly)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
codegen
devserver
example
wasm
.gitignore
Makefile
README.rst
UNLICENSE
cssstyledeclaration.go
document.go
dom.go
domrect.go
element.go
event.go
eventtarget.go
extra.go
history.go
htmlelement.go
htmlinputelement.go
keyboardevent.go
location.go
navigator.go
node.go
tokenlist.go

README.rst

DOM Manipulation in Go

https://godoc.org/github.com/siongui/godom?status.png https://api.travis-ci.org/siongui/godom.png?branch=master https://goreportcard.com/badge/github.com/siongui/godom

https://img.shields.io/twitter/url/https/github.com/siongui/godom.svg?style=social

Make DOM Manipulation in Go as similar to JavaScript as possible via GopherJS. For DOM Manipulation via WebAssembly, visit wasm directory.

Why?

Why not use GopherJS directly?

Because the code written directly by GopherJS without any wrapper is really ugly. For example, if you want to getElementById, you need to write:

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

foo := js.Global.Get("document").Call("getElementById", "foo")

With godom, you write:

import (
      . "github.com/siongui/godom"
)

foo := Document.GetElementById("foo")

which looks like JavaScript and more readable.

Why not use existing go-js-dom?

Because it's too restricted, and sometimes need to do a lot of type casting. For example, if you have an audio element with id foo and you want to call the Play() method, you need to write the following code:

import (
      "honnef.co/go/js/dom"
)

a := dom.GetWindow().Document().GetElementByID("foo").(*dom.HTMLAudioElement)
a.Play()

If you use querySelectorAll to select a lot of such elements, you need to do a lot of type casting, which is really disturbing.

With godom, you can write like this:

import (
      . "github.com/siongui/godom"
)

a := Document.GetElementById("foo")
a.Play()

What if the method/property is not implemented in godom?

godom is only a wrapper for GopherJS. If something is not implemented, you can still use the GopherJS methods to call or get the method/property you need. For example, if the Play() method of the audio element is not implemented, you can use GopherJS Call method to call play method directly:

import (
      . "github.com/siongui/godom"
)

a := Document.GetElementById("foo")
a.Call("play")

Code Example

Issues

null test

Test if event.state is null in popstate event listener:

ih := Document.QuerySelector("#infoHistory")

Window.AddEventListener("popstate", func(e Event) {
        if e.Get("state") == nil {
                ih.SetInnerHTML("Entry Page")
        } else {
                ih.SetInnerHTML(e.Get("state").String())
        }
})

HTML dataset (data-* attribute)

Assume we have the following element:

<p id="foo" data-content="content of person 1"></p>

You can access the data-content as follows:

p := Document.QuerySelector("#foo")
content := p.Dataset().Get("content").String()

UNLICENSE

Released in public domain. See UNLICENSE.

References

[1]GopherJS - A compiler from Go to JavaScript (GitHub, GopherJS Playground, godoc)
[2]dom - GopherJS bindings for the JavaScript DOM APIs (GitHub)
[3]
[4][Golang] Add Method to Existing Type in External Package
[5]JavaScript Remove All Children of a DOM Element
[6]How to do insert After() in JavaScript without using a library? - Stack Overflow
[7]

javascript element position

javascript - Retrieve the position (X,Y) of an HTML element - Stack Overflow

[8]

javascript check class exists - Google search

javascript - Test if an element contains a class? - Stack Overflow

[9]
[10]Go Report Card | Go project code quality report cards
[11]Shields.io: Quality metadata badges for open source projects
[12]HTML DOM Style object
[13]