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">
- Its value is the name of any DOM event:
click,keyup,input,change,mouseenter,submit. - htmx also has events of its own:
load,revealed,intersect, andeveryfor polling. - After the event name, add modifiers separated by spaces:
keyup changed delay:500ms. - Several triggers are separated by commas:
load, every 30s.
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">
delay:500ms: everykeyupstarts a 500 ms countdown. If a newkeyupcomes before it runs out, the countdown starts again from zero. The request goes out 500 ms after the last key. This technique is called debounce.changed: an event only counts when the value differs from last time. Shift, the arrows or Ctrl don't change the value, so they're ignored and don't restart the countdown.- The unit is
msors:delay:1sis the same asdelay:1000ms.
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>
every 30ssendsGET /orderevery 30 seconds, and the reply (the latest<li>rows) replaces the board's content. Statuses Mrs. Rini changed in the back show up too.- The first request goes out 30 seconds after the page opens, not right away. To also load right away, write
hx-trigger="load, every 30s". - Polling stops by itself when the element is removed from the page.
- Count the cost: one open tab = 120 requests an hour. Ten tabs = 1,200.
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>
from:#reload: the board listens for clicks on another button. A click on the board itself no longer triggers anything, because its defaultclickwas replaced. Besides a CSS selector,from:takesdocument,window,closest ...andfind ....once: the request is only sent on the first event. The cursor entering card #12 a second time sends nothing.
load, revealed, intersect
load: once, as soon as the element is loaded. The#summarybox in the hx-get lesson uses it. It can wait:load delay:1s.revealed: once, when the element first comes on screen because of scrolling. Good for loading the bottom of a page later.intersect: every time the element enters the viewport, with options such asthreshold:0.5(half of it visible). Both come back in the infinite scroll lesson.
⚠️ Too many requests, late ones, or none at all
- An empty or misspelt trigger.
hx-trigger=""falls back to the default, andhx-trigger="clcik"waits for an event that never happens. There's no error. In the exercise, the<input>needshx-trigger="keyup changed delay:300ms"and the<div>needshx-trigger="mouseenter once". keyupwithoutdelay. One request per letter, and the server carries all of them.throttlefor search. The last keystrokes are dropped. Search usesdelay.- Polling without
load. The board only fills 30 seconds after the page opens. - Forgetting
once. Every time the cursor enters, the request is sent again.
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.