Skip to content

HTML Element Prototypes

HTML values expose attribute and children helpers.

let btn = <button>Click</button>
btn.setAttribute("id", "main-button")
println(btn.getAttribute("id"))

Store a literal in a variable before calling its methods. Direct prototype calls on literals are limited by the current parser.

MethodReturnsDescription
setAttribute(name: string, value: any)voidSets an attribute.
removeAttribute(name: string)voidRemoves an attribute.
getAttribute(name: string)anyReturns an attribute value, or nil.
hasAttribute(name: string)boolChecks whether an attribute exists.
children()[](html | string)Returns the element’s children.
setChildren(children: [](html | string))voidReplaces the element’s children.

HTML elements also support indexed access through internal __get__ and __set__.

let btn = <button>Click</button>
btn["id"] = "save-button"
println(btn["id"])

Attribute names are normalized to kebab-case.

let btn = <button>Click</button>
btn.setAttribute("dataUserId", "123")
println(btn.getAttribute("data-user-id"))
let btn = <button>Click</button>
btn.setAttribute("class", "primary")
btn.setAttribute("disabled", true)
println(btn.hasAttribute("class"))
println(btn.getAttribute("class"))
btn.removeAttribute("disabled")
let box = <div>Hello</div>
let children = box.children()
println(children)
let box = <div>Hello</div>
let children: [](html|string) = []
children.push(<span>Nubo</span>)
children.push("!")
box.setChildren(children)
println(box.children())