Native Tags

Native tags are built-in HTML, SVG, and MathML elements. In Marko they follow standard HTML semantics with targeted ergonomic enhancements.

Attributes on native tags (including custom elements registered with "html": true) are written via setAttribute, except for Marko's built-in controllable bindings. To assign JavaScript properties directly on a custom element, use an element reference inside <script>.

Element References

Every native tag exposes a tag variable returning a getter for its DOM node:

<div/ref/>

<script>
  ref().innerHTML = "Hello World";
</script>
div/ref

script
  --

    ref().innerHTML = "Hello World";

  --
Caution

DOM nodes exist only in the browser, and element getters are readable only in post-render code: <script> bodies, <lifecycle> hooks, and event handlers. Attributes, <const>, and interpolations evaluate during render.

Enhanced Attributes

class=

Accepts strings, objects, and arbitrarily nested arrays:

// string
<div class="a c"/>

// object
<div class={ a: true, b: false, c: true }/>

// array
<div class=["a", null, { c: true }]/>
// string
div class="a c"

// object
div class={ a: true, b: false, c: true }

// array
div class=["a", null, { c: true }]

All three render the same HTML:

<div class="a c"></div>

Object keys are class names included when their value is truthy (one level deep, never recursively traversed). Array entries may nest and spread, skipping falsy entries. Each class name may appear only once across a class= value (except across mutually exclusive ternary branches), because Marko updates individual toggled classes independently.

<let/query="">

<input
  value:=query
  class=["field", query && ["field-filled", { "field-error": !query.trim() }]]
>
let/query=""

input
  ,value:=query
  ,class=["field", query && ["field-filled", { "field-error": !query.trim() }]]

style=

Accepts strings, objects, and arbitrarily nested arrays:

// string
<div style="display:block;margin-right:16px"/>

// object
<div style={ display: "block", "margin-right": "16px" }/>

// array
<div style=["display:block", null, { "margin-right": "16px" }]/>
// string
div style="display:block;margin-right:16px"

// object
div style={ display: "block", "margin-right": "16px" }

// array
div style=["display:block", null, { "margin-right": "16px" }]

All three render display:block;margin-right:16px (joined with ;, no trailing ;).

Object keys write verbatim as hyphen-case CSS property names.

Warning

Unlike the DOM style API, keys are not converted from camelCase:

// ❌ renders `backgroundColor:red`
<div style={ backgroundColor: "red" }/>

// ✅
<div style={ "background-color": "red" }/>
// ❌ renders `backgroundColor:red`
div style={ backgroundColor: "red" }

// ✅
div style={ "background-color": "red" }

Values stringify verbatim with no unit inference ({ "margin-right": 16 } renders margin-right:16, not 16px). Use numbers only for unitless properties (line-height, z-index) or 0.

A declaration is dropped when its value is false, null, undefined, "", NaN, or 0n, matching skipped dynamic text values. 0 is kept ({ width: 0 } renders width:0).

Tip

TypeScript rejects false as a style object property value, so place conditional declarations inside an array:

<div style=["display:block", isError && { color: "red" }]/>
div style=["display:block", isError && { color: "red" }]

Arrays nest and spread as in class=, and each CSS property may appear only once in a style= value. Custom properties (--foo) write verbatim and can be typed via Marko.CSS.Properties.

content=

Native elements that accept child markup accept body content via content=. The value may be a <define> variable, an imported template, an attribute tag carrying content, a falsy value, or the surrounding template's input.content.

Because content is part of input, a spread forwards both attributes and children:

tags/field-row.marko
<fieldset ...input/>
fieldset ...input
signup.marko
<field-row class="row">
  <label for="email">Email</label>
  <input id="email" name="email" type="email">
</field-row>
field-row class="row"
  label for="email" -- Email
  input id="email" name="email" type="email"

Pass content= explicitly to target an inner wrapper element:

<section class="card">
  <h2>${input.title}</h2>
  <div class="card-body" content=input.content/>
</section>
section class="card"
  h2 -- ${input.title}
  div class="card-body" content=input.content

content= is reactive; swapping the value replaces the inner content without recreating the host element:

<let/expanded=false>
<define/Summary>
  <h2>${input.title}</h2>
</define>
<define/Details>
  <h2>${input.title}</h2>
  <p>${input.description}</p>
</define>

<article content=(expanded ? Details : Summary)/>
<button onClick() { expanded = !expanded }>Toggle</button>
let/expanded=false
define/Summary
  h2 -- ${input.title}
define/Details
  h2 -- ${input.title}
  p -- ${input.description}

article content=(expanded ? Details : Summary)
button onClick() { expanded = !expanded } -- Toggle
Warning

Any literal tag body takes precedence over content=, including a body containing only a comment:

<div content=Summary>
  // ❌ this comment is body content, so `Summary` never renders
</div>
div content=Summary
  // ❌ this comment is body content, so `Summary` never renders

Attributes merge left to right: placing content= after a spread overrides forwarded content, and content=undefined drops forwarded content while keeping all other attributes:

<div ...input content=undefined/>
div ...input content=undefined

Void elements (<img>, <input>, etc.) and raw-text elements (<textarea>, <title>, <html-script>, <html-style>) do not accept content= as child markup.

Note

On <meta>, content keeps its standard HTML attribute meaning, including through spreads:

<let/height=630>
<meta property="og:image:height" content=height>
let/height=630
meta property="og:image:height" content=height

See Typing content for TypeScript types.

Event Handlers

Attributes beginning with on followed by - or a capital letter register event listeners. on- preserves exact event casing, while onCamelCase lowercases the event name (in TypeScript, standard DOM events use exact camelCase spellings such as onKeyDown):

  • onDblClick → dblclick
  • on-DblClick → DblClick
<button onClick() { alert("Hi!") }>Say Hi</button>

// equivalent to
<button on-click() { alert("Hi!") }>Say Hi</button>
button onClick() { alert("Hi!") } -- Say Hi

// equivalent to
button on-click() { alert("Hi!") } -- Say Hi

Values must be a function or a falsy value, enabling conditional listeners:

<let/clicked=false>
<button onClick=!clicked && (() => {
  alert("First click!");
  clicked = true;
})>
  Click me!
</button>
let/clicked=false
button onClick=!clicked && (() => {
  alert("First click!");
  clicked = true;
}) --
  Click me!
Tip

Use on- for custom elements that dispatch mixed-case event names.

Caution

Avoid native inline HTML string handlers; they bypass Marko reactivity and conflict with strict CSP policies:

<button onclick="this.innerHTML++">0</button>
button onclick="this.innerHTML++" -- 0

Handler Arguments

Every event handler receives (event, target): the Event and the element carrying the on* attribute:

<form onSubmit(event, form) {
  event.preventDefault();
  fetch("/subscribe", { method: "POST", body: new FormData(form) });
}>
  <input name="email" type="email">
  <button>Subscribe</button>
</form>
form onSubmit(event, form) {
  event.preventDefault();
  fetch("/subscribe", { method: "POST", body: new FormData(form) });
}
  input name="email" type="email"
  button -- Subscribe

Unlike event.target (which may be a clicked descendant element), the second parameter is always the element with the handler attached. Because handlers are delegated on document, use the second argument or an element reference rather than event.currentTarget.

Delegation

Marko attaches a single capture-phase listener per event type on document and dispatches matching handlers from the event target upward through bubbling ancestors:

  • Marko handlers run before listeners attached with addEventListener on the element or any descendant of document.
  • Calling event.stopPropagation() inside a Marko handler stops ancestor Marko handlers as well as addEventListener listeners below document (including on the same element). Calling it in an addEventListener listener below document does not affect Marko handlers, which have already run.
  • Non-bubbling events (focus, blur, load) invoke only the handler on the target element.
  • Events inside a Shadow DOM retarget to the host before reaching document, so Marko's delegated handlers operate on light DOM trees.

Enhanced Value Attributes

Marko adds uniform state attributes to native form controls whose DOM state is normally split across child elements or properties.

Radio and Checkbox

<input type="radio"> and <input type="checkbox"> support checkedValue=, marking the input checked whenever checkedValue= matches value=:

  • String checkedValue=: selects one matching radio input (keep name= for native form submission and keyboard navigation).
  • String array checkedValue=: checks every checkbox whose value= is in the array.

<select>

<select> supports value= as a string (single select) or string array (<select multiple>), setting selected on matching <option> elements using string comparison (value=25 matches <option value="25">; null or undefined matches an empty value=).

When <select> uses value= or valueChange=, every nested <option> (including inside <optgroup> or <for>) must declare its own value=, and initial selection comes from the <select> rather than selected= on <option>.

<textarea>

<textarea> accepts its value through either its body or value= (not both). Static <textarea> body text decodes HTML character references and re-escapes through the value path.

Change Handlers

Native controls that maintain internal browser state (<input>, <select>, <textarea>, <details>, <dialog>) are uncontrolled by default: setting value= writes only the HTML default attribute while the browser owns edits.

Adding a matching *Change attribute makes the control controlled by Marko state. Every user edit invokes the handler, and the DOM reflects the bound state:

// Uncontrolled: browser owns user edits; later `value=` updates do not overwrite them
<input value="hello">

// Controlled with two-way binding (`:=`)
<let/message="hello">
<input value:=message>
<div>${message}</div>
<button onClick() { message = "goodbye" }>Set to goodbye</button>
// Uncontrolled: browser owns user edits; later `value=` updates do not overwrite them
input value="hello"

// Controlled with two-way binding (`:=`)
let/message="hello"
input value:=message
div -- ${message}
button onClick() { message = "goodbye" } -- Set to goodbye

Use an explicit change handler to normalize, validate, or mask input before storing it:

<let/cardNumber="5555 5555 5555 5555">
<input
  value=cardNumber
  valueChange(v) {
    cardNumber = [...v.replace(/\D/g, "").matchAll(/\d{1,4}/g)].join(" ");
  }
>
let/cardNumber="5555 5555 5555 5555"
input
  ,value=cardNumber
  ,valueChange(v) {
    cardNumber = [...v.replace(/\D/g, "").matchAll(/\d{1,4}/g)].join(" ");
  }

<input>

Supports valueChange= (for editable input types), checkedChange=, and checkedValueChange=:

<let/text="">
<input type="text" value:=text>
<input type="text" value=text valueChange(v) { text = v.toLowerCase() }>
let/text=""
input type="text" value:=text
input type="text" value=text valueChange(v) { text = v.toLowerCase() }
Caution

<input> always delivers string values. Convert numeric inputs in the handler or with a refining function:

<let/count=0>

// ❌ sets `count` to a string on update
<input type="number" value:=count>

// ✅ converts the string value to a number
<input type="number" value:parseFloat:=count>
let/count=0

// ❌ sets `count` to a string on update
input type="number" value:=count

// ✅ converts the string value to a number
input type="number" value:parseFloat:=count

Checkbox checked= binding:

<let/agreed=false>
<input type="checkbox" checked:=agreed>
let/agreed=false
input type="checkbox" checked:=agreed

Radio and multi-checkbox checkedValue= binding (a handler that ignores the new value keeps the current selection):

<let/speed="ground">
<label>
  <input type="radio" name="speed" value="ground" checkedValue:=speed>
  Ground
</label>
<label>
  <input type="radio" name="speed" value="overnight" checkedValue:=speed>
  Overnight
</label>

<let/toppings=["cheese"]>
<label>
  <input type="checkbox" value="cheese" checkedValue:=toppings>
  Cheese
</label>
<label>
  <input type="checkbox" value="olives" checkedValue:=toppings>
  Olives
</label>
let/speed="ground"
label
  input type="radio" name="speed" value="ground" checkedValue:=speed
  -- Ground
label
  input type="radio" name="speed" value="overnight" checkedValue:=speed
  -- Overnight

let/toppings=["cheese"]
label
  input type="checkbox" value="cheese" checkedValue:=toppings
  -- Cheese
label
  input type="checkbox" value="olives" checkedValue:=toppings
  -- Olives

<select> (valueChange=)

Supports valueChange= for single strings or string arrays (multiple):

<let/language="en">
<select value:=language>
  <option value="en">English</option>
  <option value="pt-br">Portuguese (Brazil)</option>
  <option value="it">Italian</option>
</select>
let/language="en"
select value:=language
  option value="en" -- English
  option value="pt-br" -- Portuguese (Brazil)
  option value="it" -- Italian
<let/topics=["runtime"]>
<select multiple value:=topics>
  <option value="runtime">Runtime</option>
  <option value="compiler">Compiler</option>
  <option value="tooling">Tooling</option>
</select>
let/topics=["runtime"]
select multiple value:=topics
  option value="runtime" -- Runtime
  option value="compiler" -- Compiler
  option value="tooling" -- Tooling

<textarea> (valueChange=)

Supports valueChange= paired with value=:

<let/notes="">
<textarea value:=notes/>
let/notes=""
textarea value:=notes

<details>

Supports openChange= paired with open=. Without openChange=, open= applies only on initial render:

<let/open=false>
<details open:=open>
  <summary>Details</summary>
  <p>Expanded content</p>
</details>
<button onClick() { open = false }>Collapse</button>
let/open=false
details open:=open
  summary -- Details
  p -- Expanded content
button onClick() { open = false } -- Collapse

<dialog>

Supports openChange= paired with open= for non-modal dialogs:

<let/open=false>
<dialog open:=open>Hello!</dialog>
<button onClick() { open = !open }>Toggle</button>
let/open=false
dialog open:=open -- Hello!
button onClick() { open = !open } -- Toggle
Warning

openChange= observes the open attribute via MutationObserver and controls non-modal dialogs only. Calling .showModal() sets open and conflicts with a controlled openChange= binding. Manage modal dialogs directly with .showModal() and .close() without openChange=.

Form Reset

Resetting a form (<button type="reset"> or form.reset()) restores each controlled form element to its initial rendered value and invokes its change handler with that restored value so bound state stays synchronized:

<let/tracking="1Z999AA10">

<form>
  <input value:=tracking>
  <button type="reset">Reset</button>
</form>

<div>${tracking}</div>
let/tracking="1Z999AA10"

form
  input value:=tracking
  button type="reset" -- Reset

div -- ${tracking}
Note

Reset change handlers run on the next animation frame. Calling preventDefault() on the reset event cancels both the reset and the handler calls.

Attribute Spreads

A spread on a native tag owns the element's attribute set: attributes removed from the spread object are removed from the DOM on update:

<let/link={ href: "/report.csv", download: "report.csv" }>

<a ...link>Quarterly report</a>
<button onClick() { link = { href: "/report.csv" } }>Open in browser</button>
let/link={ href: "/report.csv", download: "report.csv" }

a ...link -- Quarterly report
button onClick() { link = { href: "/report.csv" } } -- Open in browser
Warning

Attribute removal covers every attribute on the element, including attributes added outside Marko. Place attributes that must persist either inside the spread object or after the spread.

Attributes written after a spread are excluded from the spread's ownership, letting wrapper tags pin or compose attributes while forwarding input:

<a ...input class=["external", input.class] target="_blank" rel="noreferrer"/>
a ...input class=["external", input.class] target="_blank" rel="noreferrer"

Attributes written before a spread merge into the spread object and are overridden by matching keys in the spread. class=, style=, and change handlers retain their special handling inside spreads. An event handler written after a spread claims that event so the spread does not attach a duplicate listener, while still exposing the original handler on input.

<let/note="">
<const/composer={ rows: 4, value: note, valueChange(next) { note = next } }>

<textarea ...composer/>
let/note=""
const/composer={ rows: 4, value: note, valueChange(next) { note = next } }

textarea ...composer

Enhanced Tags

Three HTML constructs have enhanced behavior in Marko, with raw fallbacks when literal HTML output is needed.

<script>

Marko's <script> runs reactive browser effects. Emit a literal HTML <script> element with <html-script>:

<html-script type="application/json">
  { "foo": ["bar", "baz"] }
</html-script>
html-script type="application/json"
  --

    { "foo": ["bar", "baz"] }

  --

<style>

Marko's <style> extracts and bundles CSS. Emit a literal inline HTML <style> element with <html-style>.

<!-- comment -->

Marko strips comments by default. Emit a literal <!-- comment --> with <html-comment>.


Contributors

Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.