Dom
Dom — browser DOM bindings for March compiled with --target js.
Provides typed wrappers over the browser's Document Object Model. Requires --target js; calling these functions from a native build panics.
Usage: match Dom.find("counter") do None -> () Some(el) -> Dom.set_text(el, "0") Dom.on(el, "click", fn _ -> Dom.set_text(el, "clicked!")) end
Note: set_timeout/set_interval/on_frame take a zero-arg callback typed Unit -> Unit — pass a 0-arg lambda fn -> .... (The typechecker accepts fn -> body against a Unit -> Unit parameter, treating it as a unit-consuming thunk.)
Functions
Add CSS class cls to el's classList. No-op if already present.
Show a blocking browser alert() dialog with msg.
Append child as the last child of parent. If child is already in the tree it is moved.
The document <body> element.
Return a List(Node) of the direct element children of el (text nodes excluded).
Remove all child nodes from el, leaving it empty.
Return a deep clone of el (all descendants included), detached from the tree.
Create a new element with the given HTML tag name (e.g. \
Return the key that triggered a `\
Return the element that triggered ev (the event.target node).
Return the event type string for ev (e.g. \
Return the x-coordinate of ev relative to the target element (event.offsetX). Useful for hit-testing pointer/click events against a <canvas>.
Return the y-coordinate of ev relative to the target element (event.offsetY).
Look up an element by its id attribute. Returns None if no element with that id exists.
Return the first element child of el, or None if el has no element children.
Return the value of the named attribute, or None if the attribute is absent.
Return the innerHTML of el as a string.
Return the inline CSS value for prop on el. Returns an empty string if the property is not set inline.
Return the textContent of el (all descendant text, concatenated).
Return the current value property of a form input, textarea, or select element.
Return true if el has the named attribute, false otherwise.
Return true if el's classList contains cls.
Return the current page URL (window.location.href).
Drain and return the keyboard keys (keydown event.key strings, e.g. \
Return the last element child of el, or None if el has no element children.
Attach handler to el for DOM event event (e.g. \
Schedule cb to run before the next browser repaint (requestAnimationFrame). Use for smooth 60 fps animations. cb is a zero-arg callback — pass fn -> ....
Return the direct parent element of el, or None if el is the root or detached.
The live cursor position over el as element-relative (x, y) integers, updated by a mousemove listener installed on the first call. Defaults to el's center before any movement. Unlike taps, this reads the current position rather than draining a buffer.
Insert child before all existing children of parent. If child is already in the tree it is moved.
Prevent the browser's default action for ev (e.g. stop a link from navigating, a form from submitting).
Detach el from its parent and remove it from the DOM. No-op if el has no parent.
Remove the named attribute from el. No-op if the attribute is absent.
Remove child from parent. Panics if child is not a direct child of parent; prefer remove when the parent is unknown.
Remove CSS class cls from el's classList. No-op if absent.
Find the first element matching a CSS selector string (e.g. \
Find all elements matching a CSS selector. Returns a List(Node), empty when nothing matches.
Set the value of the named attribute on el (e.g. `set_attr(el, \
Set the innerHTML of el. The string is parsed as HTML; use set_text when the content is plain text.
Call cb repeatedly every ms milliseconds. Equivalent to setInterval. Returns no handle — cancel by keeping a flag in the DOM. cb is a zero-arg callback — pass fn -> ....
Set an inline CSS property on el (e.g. `set_style(el, \
Set the textContent of el. Replaces all children with a single text node; HTML is not interpreted.
Call cb once after ms milliseconds (non-blocking, runs on the event loop). Equivalent to setTimeout. cb is a zero-arg callback — pass fn -> ....
Set the value property of a form input, textarea, or select element.
Stop ev from bubbling up to parent elements.
Read key from the browser's localStorage. Returns None if the key is absent or storage is unavailable (e.g. private browsing).
Write val under key in the browser's localStorage. Silently does nothing if storage is unavailable.
Drain and return the pointer taps (pointerdown events) on el since the last call, as element-relative (x, y) integer pairs. The first call installs the buffering listener. Poll this once per animation frame for game input.
Create a text node containing the given string. Use append to attach it to an element.
Toggle CSS class cls on el: add it if absent, remove it if present.
Remove a previously attached event listener. handler must be the same function value passed to listen.
The browser window's inner size (window.innerWidth/innerHeight) in CSS pixels, as (w, h). No devicePixelRatio handling.