All you want is one small thing: a customer name search box that shows results as you type, with no Search button. The tutorial you find starts with installing React, setting up state, then writing a fetch. For one box, that's too much. In htmx, this box is just an <input> with a few attributes you already know.
The pattern goes by several names: active search, live search, instant search. It's the same thing. The cashier at Mrs. Rini's Laundry does it every day with the order book. She waits for the customer to finish saying the name, then turns the pages. If the customer corrects themselves halfway ("Wu... no, Wulan"), a smart cashier stops looking for "Wu" and looks for "Wulan" straight away. If the customer says "never mind", the open page gets closed again.
The pattern: one box, one result list
<input id="search-box" name="q" type="search" placeholder="Customer name"
hx-get="/search"
hx-trigger="keyup changed delay:500ms, search"
hx-target="#results"
hx-indicator="#searching"
hx-sync="this:replace">
<span id="searching" class="htmx-indicator">Searching...</span>
<ul id="results"></ul>
hx-get="/search": the box's value goes along as the query,GET /search?q=wulan, because itsnameisq.hx-trigger: when the request goes out. Covered in the next two sections.hx-target="#results": the reply goes into the list, not over the box.hx-indicator="#searching": "Searching..." shows while the request runs (the loading indicator lesson).hx-sync="this:replace": an old request is cancelled when a new one comes. Covered in "Stale replies and hx-sync".
The server replies with <li> rows only, not a whole page:
<li>#8 Ms. Wulandari, 2 kg: Picked up</li>
<li>#13 Ms. Wulan, 2 kg: Ironing</li>
Typing "wulan": one request
From the hx-trigger lesson: a bare keyup sends one request per letter. delay:500ms waits for 500 ms without a keystroke, and changed ignores keys that don't change the value (Shift, arrows). Typing wulan at 120 ms per letter:
| Time | What happens |
|---|---|
| 0 to 480 ms | w, u, l, a, n: each letter restarts the 500 ms countdown |
| 980 ms | GET /search?q=wulan, the only request |
| 1,130 ms | the reply arrives, 2 rows go into #results |
- The event can be
keyuporinput.inputalso catches text pasted with a right click. - The pause is your choice. 300 ms feels snappier, 500 ms saves more requests. The exercise uses
delay:300ms.
The × button and the search event
A type="search" box in Chrome, Edge and Safari has an × to clear it. Clicking × does not fire keyup, because no keyboard key is released. So the box is empty, but the "wulan" results stay on screen. The list is stale, and the user is confused.
The same browsers fire a search event when × is clicked and when Enter is pressed. Add it as a second trigger:
hx-trigger="keyup changed delay:500ms, search"
- × clicked:
searchsendsGET /search?q=at once (no pause, becausesearchhas nodelay), the server replies with nothing, and#resultsempties too. - Enter pressed before the 500 ms run out: the running countdown is cancelled and the request goes out right away. Still one request.
- Firefox doesn't fire
search. If you need every browser, useinput changed delay:500ms: × firesinput, so the list empties 500 ms later.
Stale replies and hx-sync
Say the server needs 1,200 ms for wu (a short word, lots to match) and 150 ms for wulan. You type wu, stop for 0.7 seconds, then go on with lan:
| Time | Without hx-sync |
With hx-sync="this:replace" |
|---|---|---|
| 620 ms | GET /search?q=wu sent |
same |
| 700 to 940 ms | l, a, n typed |
same |
| 1,440 ms | wulan is ready to go, but wu has no reply yet: wulan waits in line |
wu is cancelled, GET /search?q=wulan goes out at once |
| 1,590 ms | the box says "wulan", the list is still empty | the wulan results show, done |
| 1,820 ms | the stale wu results show (3 rows), only now wulan is sent |
|
| 1,970 ms | the wulan results show |
- htmx's default: a new request from the same element waits in line behind the one still running. The order never flips, but stale results show for a moment and the right ones arrive 380 ms late.
hx-sync="this:replace": the running request is cancelled (aborted) and the new one goes out at once. Stale results never show.- If you write your own
fetch(), both requests run side by side, and the slowwureply can arrive afterwulanand overwrite it. That's the classic race condition.
The value of hx-sync is the element to sync on, a colon, then the strategy:
| Strategy | When a request is already running |
|---|---|
replace |
the old one is cancelled, the new one is sent. The pick for search |
drop |
the new one is thrown away. This is the default when no strategy is written |
abort |
the new one is thrown away, like drop. The difference: this request gets cancelled when the element it syncs on (a form, say) sends another request |
queue last |
the new one waits in line, only the latest is kept |
hx-sync="this" with no strategy means drop: wulan is thrown away, and the list stops at the wu results.
throttle: why it doesn't fit search
hx-trigger="keyup changed throttle:1s" sends the first letter at once, then throws away other keystrokes for 1 second. Typing wulan in 0.5 seconds only sends q=w, and wulan is never sent if you stop typing. throttle caps the rate, delay waits for you to finish. Search uses delay. The second exercise uses delay together with two ideas from this lesson: the search event and hx-sync="this:replace".
⚠️ Stale results, or the last keystroke thrown away
keyupwithoutdelay. One request per letter. In the exercise, the<input>needshx-trigger="keyup changed delay:300ms",hx-get="/api/search"andhx-target="#results".- Forgetting
search. The box is cleared with ×, but the old results stay on screen. hx-sync="this"with no strategy. The default isdrop, so the last keystrokes are thrown away. Writethis:replace.hx-indicatorwithout#.hx-indicator="search-spinner"looks for a<search-spinner>tag. Writehx-indicator="#search-spinner", and the element needs thehtmx-indicatorclass.
TL;DR: Active search (live search) sends the search box while you type: hx-get="/search", hx-target="#results" and hx-trigger="keyup changed delay:500ms, search". keyup (or input) triggers, changed ignores keys that don't change the value, delay waits for the last keystroke, and search catches the × button and Enter in Chrome, Edge and Safari. Without search, clearing the box with × leaves the old results on screen. hx-sync="this:replace" cancels an old request that's still running, so stale results never show. hx-sync="this" alone means drop and throws away the last keystrokes. throttle throws away the last keystrokes too, so search uses delay.
⚡ One box, one result list
Write:
<input id="search-box" name="q" type="search" placeholder="Customer name"
hx-get="/search"
hx-trigger="keyup changed delay:500ms, search"
hx-target="#results"
hx-indicator="#searching"
hx-sync="this:replace">
<span id="searching" class="htmx-indicator">Searching...</span>
<ul id="results"></ul>
Meaning: The box's value goes along as the query, GET /search?q=wulan, because its name is q. delay:500ms waits for the last keystroke, so typing "wulan" sends just one request. The server replies with <li> rows only, not a whole page.
⚡ The × button and the search event
Write:
hx-trigger="keyup changed delay:500ms, search"
Guess first: the box is cleared with the ×, and the only trigger is keyup. Why do the "wulan" results stay on screen?
Clicking × doesn't fire keyup, because no keyboard key is released. That's why search is added as a second trigger.
Meaning: Chrome, Edge and Safari fire search when × is clicked and when Enter is pressed. Firefox doesn't, so use input changed delay:500ms if you need every browser.
⚡ hx-sync: cancel stale requests
Meaning: hx-sync="this:replace" cancels an old request that's still running and sends the new one at once, so stale results never show. hx-sync="this" alone means drop, and the last keystrokes are thrown away.