Control when requests are made with various trigger events in html★.
Default Triggers
html★ uses sensible defaults based on element type:
| Element | Default Trigger |
|---|---|
<a> | click |
<button> | click |
<form> | submit |
Custom Triggers
Use data-trigger to specify a different trigger:
| Trigger | When it fires | Use Case |
|---|---|---|
click | Element is clicked | Buttons, links |
submit | Form is submitted | Forms |
change | Value changes (blur) | Selects, checkboxes |
input | Value changes (debounced) | Search inputs |
visible | Element enters viewport | Infinite scroll |
load | Immediately on init | Load on page load |
Examples
Live Search (input)
<input type="search" name="q" data-href="/search" data-target="#results" data-trigger="input" data-include="self"><div id="results"></div>
Fires on each keystroke, debounced by 300ms (configurable).
Filter Select (change)
<select name="category" data-href="/products" data-target="#products" data-trigger="change" data-include="self"> <option value="all">All</option> <option value="electronics">Electronics</option> <option value="books">Books</option></select>
Infinite Scroll (visible)
<div data-href="/posts?page=2" data-trigger="visible" data-target="#feed" data-swap="append"> Loading more...</div>
Uses IntersectionObserver. Fires once when element becomes visible.
Load on Init (load)
<div data-href="/api/status" data-trigger="load" data-target="self"> Loading status...</div>
Fires immediately when html★ initializes.
Instant Navigation
Add data-instant to trigger navigation on mousedown instead of click, making it feel ~100ms faster:
<nav data-target="#main" data-instant> <a href="/dashboard">Dashboard</a> <a href="/settings">Settings</a></nav>
data-instant inherits through the cascade, so setting it on a parent applies to all links within. Only left-clicks are handled — right-click and middle-click work normally.
Including Values
Use data-include to include input values in the request:
<!-- Include the element's own value --><input name="q" data-href="/search" data-target="#results" data-trigger="input" data-include="self"> <!-- Include multiple inputs by selector --><button data-href="/api/filter" data-target="#results" data-include="#filters input, #filters select"> Apply Filters</button>
For GET requests, included values are appended as query parameters. For POST/PUT/PATCH, they're added to the FormData body. Each included element must have a name attribute.
Custom Request Headers
Use data-headers to send additional HTTP headers as a JSON object:
<button data-href="/api/data" data-target="#output" data-headers='{"Accept": "text/html", "X-Custom": "value"}'> Fetch Data</button>
Headers are merged with html★'s default headers (X-Requested-With: htmlstar).
Trigger Modifiers
Append modifiers after the event type (space-separated) to control timing and execution:
data-trigger="<event> <modifier> <modifier> ..."
Debounce
Delay execution until input stops for the specified duration:
<input data-href="/search" data-trigger="input debounce:500" data-target="#results" data-include="self">
Default is 300ms if no value is specified: data-trigger="input debounce".
Throttle
Limit execution to at most once per interval:
<div data-href="/api/track" data-trigger="mousemove throttle:200" data-target="#coords"></div>
Default is 300ms if no value is specified.
Delay
Wait a fixed duration before executing:
<div data-href="/api/tooltip" data-trigger="mouseenter delay:500" data-target="#tooltip"> Hover me</div>
Once
Fire only once, then stop listening:
<div data-href="/api/welcome" data-trigger="visible once" data-target="self"> Loading welcome message...</div>
Combining Modifiers
Modifiers can be combined:
<!-- Debounce input AND fire only once --><input data-trigger="input debounce:500 once" data-href="/api/validate" data-target="#validation" data-include="self">
| Modifier | Syntax | Default | Description |
|---|---|---|---|
debounce | debounce:500 | 300ms | Wait for input to stop |
throttle | throttle:200 | 300ms | Rate-limit execution |
delay | delay:100 | — | Fixed delay before execution |
once | once | — | Fire only once |
Debounce Configuration
You can also configure debounce globally or via data attributes as an alternative to inline modifiers:
<!-- Via data attribute (element level) --><input data-trigger="input" data-debounce="500"> <!-- Via meta tag (global) --><meta name="htmlstar:debounce" content="200">
Inline modifiers (data-trigger="input debounce:500") take precedence over data-debounce and meta tag configuration.
Warning:
data-triggerdoes NOT inherit from ancestors. Each element must specify its own trigger if it differs from the default.