hx-trigger — HTMX

hx-trigger replaces the default event (change for inputs, submit for forms, click for the rest) with the event you choose, plus modifiers separated by spaces…

The search box on Mrs. Rini's order board already uses hx-get. The cashier types "darto", and nothing happens until she presses Enter. It isn't broken. An <input> only sends its request when its value changes and you then move to another box or press Enter. For search as you type, it's the trigger that has to change.

hx-trigger decides when an element sends its request: which event it waits for, plus extra rules such as "wait a moment first", "at most once every 2 seconds" or "only once". The cashier at Mrs. Rini's Laundry knows this well. If she flipped through the order book every time the customer said one letter ("D... a... r..."), she'd do five lookups for one name. A smart cashier waits until the customer has been quiet for half a second, then looks. The order board on the wall is also checked again every 30 seconds, without anyone asking. Both are set with hx-trigger.

The default, and how to replace it

From the hx-get lesson: an <input> waits for change (its value changes and you move to another box or press Enter), a <form> waits for submit, and every other element waits for click. hx-trigger replaces that default:

<input name="q" type="search" hx-get="/search" hx-trigger="keyup" hx-target="#results">

Searching as you type: keyup

With hx-trigger="keyup", every key you release sends a request:

You type The box's value Request
d d GET /search?q=d
a da GET /search?q=da
r dar GET /search?q=dar
t dart GET /search?q=dart
o darto GET /search?q=darto

5 letters, 5 requests. The first four are useless, and pressing Shift or the left arrow sends yet another request, even though the value is the same.

Debounce: delay and changed

<input name="q" type="search" hx-get="/search"
       hx-trigger="keyup changed delay:500ms" hx-target="#results">

Typing darto with 120 ms between letters:

Time What happens
0 ms d, the 500 ms countdown starts
120, 240, 360 ms a, r, t, the countdown restarts each time
480 ms o, the last countdown starts
980 ms GET /search?q=darto, the only request
1,400 ms Shift released, the value is still darto, ignored

input changed delay:500ms works the same, and also catches text pasted with a right click.

throttle: at most once per stretch of time

<button hx-get="/order/12/status" hx-trigger="click throttle:2s" hx-target="#status-12">
  Check status
</button>

The first click sends a request right away. Other clicks in the next 2 seconds are dropped, not postponed. It suits a button people tend to click again and again. For search, throttle is the wrong pick: typing darto within 2 seconds only sends q=d, and darto is never sent.

Polling: every

<ul id="order-board" hx-get="/order" hx-trigger="every 30s">
  <li id="order-12">#12 Mr. Darto, 3 kg: <span>Washing</span></li>
  ...
</ul>

from: and once

<button id="reload">Reload</button>
<ul id="order-board" hx-get="/order" hx-trigger="every 30s, click from:#reload">...</ul>

<li id="order-12" hx-get="/order/12/history" hx-trigger="mouseenter once">...</li>

load, revealed, intersect

⚠️ Too many requests, late ones, or none at all

TL;DR: hx-trigger replaces the default event (change for inputs, submit for forms, click for the rest) with the event you choose, plus modifiers separated by spaces. keyup changed delay:500ms sends one request 500 ms after the last keystroke, and only when the value changed. throttle:2s sends right away, then drops further events for 2 seconds. every 30s polls every 30 seconds, starting 30 seconds after the page opens (load, every 30s to start right away). from:#reload listens to another element, once fires just once, load fires on load, revealed and intersect fire when the element shows on screen. A misspelt trigger fires nothing, with no error.

⚡ Replace the default trigger

Write:

<input name="q" type="search" hx-get="/search" hx-trigger="keyup" hx-target="#results">
Guess first: you type darto into this box. How many requests are sent?

5, one per letter. The first four are useless, and pressing Shift or the left arrow sends a request too.

Meaning: hx-trigger decides when the request is sent. Its value is the name of any event, such as click, keyup, input or mouseenter. A misspelt trigger fires nothing, with no error.

⚡ Debounce: changed and delay

Write:

<input name="q" type="search" hx-get="/search"
       hx-trigger="keyup changed delay:500ms" hx-target="#results">

Meaning: Every keyup restarts a 500 ms countdown from zero, so the request goes out 500 ms after the last keystroke. changed ignores keys that don't change the value, such as Shift and the arrows.

⚡ Polling, once and from:

Write:

<ul id="order-board" hx-get="/order" hx-trigger="every 30s">

Meaning: every 30s sends a request every 30 seconds, starting 30 seconds after the page opens. Write load, every 30s to load right away too. once fires just once, and from:#reload listens for clicks on another element.

What you will learn