The cashier presses Take in laundry. The server is slow today, so for one second the page doesn't change at all. The cashier thinks the click didn't register and presses again. Now Mrs. Tini has two orders. Two things are missing here: a sign that the page is working, and a button that can't be pressed twice.
A loading indicator is an element, such as a "Saving..." note or a spinner, that's only visible while a request runs. htmx handles it with one class: while a request runs, htmx-request is put on, and as soon as the reply arrives, that class comes off. Mrs. Rini does the same at her counter. While she writes an order in the book, she flips a little sign that says "One moment, recording", and she covers the intake bell so nobody rings it twice. The little sign is hx-indicator, covering the bell is hx-disabled-elt.
The htmx-request class and htmx's built-in CSS
When htmx starts sending a request, the element sending it gets the htmx-request class. When the reply arrives, the class comes off. htmx also adds this CSS to your page's <head> by itself:
.htmx-indicator { opacity: 0; visibility: hidden; }
.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator { opacity: 1; visibility: visible; transition: opacity 200ms ease-in; }
- An element with the
htmx-indicatorclass is hidden. - It appears (fading in over 200 ms) when its parent, or the element itself, is wearing
htmx-request.
So a spinner inside a button works straight away, with no extra attribute:
<button hx-get="/order/12/status" hx-target="#status-12">
Check status <span class="htmx-indicator">loading...</span>
</button>
The button gets htmx-request, and the <span> inside it appears along with it.
hx-indicator: point at the indicator
The "Saving..." note at Mrs. Rini's counter sits outside the form. The form points at it:
<form id="intake" hx-post="/order" hx-target="#receipt" hx-indicator="#loading">
<input name="name">
<input name="weight">
<button>Take in laundry</button>
</form>
<span id="loading" class="htmx-indicator">Saving...</span>
<div id="receipt"></div>
hx-indicator="#loading": thehtmx-requestclass goes on#loadingitself, not on the form. The second rule in the built-in CSS (.htmx-request.htmx-indicator) is what shows it.- Its value is a CSS selector (use
#for an id), or a relative keyword such asclosest formorfind .spinner.
With a server that replies in 1 second:
| Time | #loading's class |
What you see |
|---|---|---|
| before the click | htmx-indicator |
hidden |
| 0 ms, Take in laundry pressed | htmx-indicator htmx-request |
"Saving..." appears |
| 1,000 ms, the reply arrives | htmx-indicator |
"Saving..." disappears, the receipt shows in #receipt |
hx-disabled-elt: stop double clicks
htmx doesn't throw away a second click that comes while the first request is still running. It holds it, then sends it as soon as the first reply arrives. So two clicks send 2 POSTs, and the server records orders #15 and #16 for the same Mrs. Tini. Three clicks still make 2, because htmx keeps only one request in line, the last one.
<form id="intake" hx-post="/order" hx-target="#receipt"
hx-indicator="#loading" hx-disabled-elt="find button">
hx-disabled-elt="find button": while the request runs, the<button>inside the form gets thedisabledattribute. A disabled button can't be clicked, and Enter in a field doesn't send the form either. The result is just order #15.- As soon as the reply arrives, htmx removes the
disabledit put on. - Its value is a CSS selector,
this, or a keyword such asfind,closest,next,previous. You can list several, separated by commas:find input, find button.
Styling it yourself with htmx-request
You can use the htmx-request class in your own CSS. For example, the polling order board is dimmed while it waits for its reply:
#order-board.htmx-request { opacity: 0.5; }
On your own computer the server usually answers in a few milliseconds, so the indicator just blinks. To try it, pick "Slow 3G" in the DevTools Network tab.
⚠️ Stubborn spinners and doubled orders
- A misspelt or missing class. Without the class exactly
htmx-indicator, the built-in CSS doesn't hide it, so "Loading..." shows all the time. In the exercise, the<span id="spinner">needs the class exactlyhtmx-indicator, and itshx-get="/api/data"button needshx-indicator="#spinner". - A spinner outside the triggering element with no
hx-indicator.htmx-requestgoes on the button, and a spinner that isn't inside it doesn't appear. - Forgetting the
#.hx-indicator="loading"looks for a<loading>tag. It finds nothing, so nothing appears. The request is still sent, and the error only shows in the Console. - No
hx-disabled-elton a form that changes data. Two clicks record two orders.
TL;DR: While a request runs, htmx puts the htmx-request class on the element that sent it, or on the element hx-indicator="#loading" points at. htmx's built-in CSS hides .htmx-indicator and shows it when it or a parent wears htmx-request. So a spinner inside a button only needs the htmx-indicator class, and a spinner elsewhere is pointed at with hx-indicator (with #). hx-disabled-elt="find button" puts disabled on during the request, so a second click doesn't send a second order. Without it, the second click is held and still sent.
⚡ A spinner inside the button
Write:
<button hx-get="/order/12/status" hx-target="#status-12">
Check status <span class="htmx-indicator">loading...</span>
</button>
Meaning: While the request runs, the button wears the htmx-request class. htmx's built-in CSS hides .htmx-indicator and shows it when a parent wears htmx-request, so no extra attribute is needed.
⚡ An indicator outside: point at it with hx-indicator
Write:
<form id="intake" hx-post="/order" hx-target="#receipt" hx-indicator="#loading">
<input name="name">
<input name="weight">
<button>Take in laundry</button>
</form>
<span id="loading" class="htmx-indicator">Saving...</span>
<div id="receipt"></div>
Meaning: hx-indicator="#loading" puts htmx-request on #loading itself, so "Saving..." shows while the request runs. Don't forget the #: without it, htmx looks for a <loading> tag.
⚡ Stop double clicks
Write:
<form id="intake" hx-post="/order" hx-target="#receipt"
hx-indicator="#loading" hx-disabled-elt="find button">
Guess first: with no hx-disabled-elt, the cashier clicks Take in laundry twice. How many orders are recorded?
Two orders. htmx holds the second click, then still sends it as soon as the first reply arrives.
Meaning: hx-disabled-elt="find button" puts disabled on the button during the request, so only order #15 is recorded.