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"))

Prototype calls currently work reliably on values stored in variables.

let s = "string"
println(s.length())

Calling prototype methods directly on literals is currently limited by AST/parser behavior.

// Currently may not work:
println("string".length())

Prefer assigning the literal to a variable first.

let text = "string"
println(text.length())
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()`[](htmlstring)`
`setChildren(children: [](htmlstring))`void

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())