You put hx-get on the Check status button and click it. The server's answer does arrive. But the "Check status" label on the button disappears too, replaced by the word "Ironing", and the status box next to it doesn't change at all. It's the most common thing to happen on your first day with htmx, and the cause is one attribute you haven't written yet.
hx-get="address" makes an element send a GET request to that address when its trigger happens. GET means asking for data, it changes nothing on the server. The HTML reply goes inside the element itself, unless hx-target points somewhere else. It's like asking Mrs. Rini at the laundry counter: "How far along is order #12?" If you don't point at the board, she writes the answer on the slip you hand her, your own question slip. If you point at order #12's status box on the board, the answer goes there.
When the request is sent: natural triggers
A trigger is the event that makes htmx send the request. An event is something that happens on the page, such as a click or a box's value changing. Every element has a natural trigger:
| Element | Natural trigger | When |
|---|---|---|
<input>, <select>, <textarea> |
change |
its value changes, then you press Enter or move to another box |
<form> |
submit |
the form is submitted |
any other element: <button>, <a>, <div>, ... |
click |
it is clicked |
So <button hx-get="..."> runs on click, with no extra attribute.
Without hx-target, the reply goes into the element itself
<!-- before the click -->
<button hx-get="/order/12/status">Check status</button>
<!-- the server replies: Ironing -->
<!-- after the click -->
<button hx-get="/order/12/status">Ironing</button>
The "Check status" label is gone, replaced by "Ironing", inside the button itself. htmx replaces the innerHTML, the content between the opening and closing tags, of the element that fired the request. The button is still there, but its label is broken.
With hx-target, the reply goes where you point
This is order #12 on Mrs. Rini's Laundry board, now with a clear destination:
<li id="order-12">
#12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
<button id="check-12" hx-get="/order/12/status" hx-target="#status-12">
Check status
</button>
</li>
hx-get="/order/12/status": click the button, sendGET /order/12/status.hx-target="#status-12": the replyIroningbecomes the content of<span id="status-12">. The button isn't touched.- The value of
hx-targetis a CSS selector, and the element must already be on the page. If it isn't found, htmx sends no request at all. Other selectors (closest,next,this) are covered in the hx-target lesson.
Loading itself when the page opens
Mrs. Rini's board has a summary box that has to fill in without anyone clicking:
<div id="summary" hx-get="/summary" hx-trigger="load" hx-target="this">
Loading the summary...
</div>
hx-trigger="load"replaces the natural trigger: the request is sent once, as soon as this element is on the page. Without it, the div waits for a click, and "Loading the summary..." never changes.hx-target="this"means the target is this element itself. The result is the same as having nohx-target, but the intent is written down.- The server replies
<p>3 active orders, 1 ready for pickup</p>, and the div's content becomes that sentence. - Don't write
hx-trigger="". An empty value counts as missing, so the div goes back to waiting for a click. The hx-trigger lesson covers other triggers.
What is sent to the server
Clicking Check status above sends this request:
GET /order/12/status
HX-Request: true
HX-Trigger: check-12
HX-Target: status-12
HX-Current-URL: http://localhost:8000/board
HX-Request: truemarks a request from htmx.HX-Triggeris the id of the triggering element,HX-Targetthe id of the target, and both are only there when the element has an id.HX-Current-URLis the address of the current page.- A GET has no body. Values that go along are put in the URL:
<input name="q" hx-get="/search">holdingdartosendsGET /search?q=darto. The parameter's name comes from thenameattribute. - The server replies with a piece of HTML: not JSON, not a whole page.
⚠️ Replies that land in the wrong place, or requests that never go
- Forgetting
hx-target. The reply overwrites the button's content. In the exercise, theLoad Usersbutton needshx-target="#user-list", or its label is what gets replaced. - The server replies with a whole page. htmx drops its
<head>and puts the whole<body>content into the target, so the menu and heading show up inside the span. Reply with just the piece. - A div with no
hx-trigger. A div waits for a click. For a box that loads itself, writehx-trigger="load", and don't leave it empty. - The target isn't on the page yet. If
#user-listdoesn't exist, htmx sends nothing.
TL;DR: hx-get="/order/12/status" sends a GET request when the element's natural trigger happens: click for buttons, divs and other elements, change for inputs, selects and textareas, submit for forms. Without hx-target, the reply replaces the element's own content, so write hx-target="#status-12" to point somewhere else. hx-trigger="load" makes an element load itself when the page opens, and hx-target="this" points at the element itself. A GET carries values in the URL (/search?q=darto) and the header HX-Request: true. The server replies with a piece of HTML, not a whole page.
⚡ Without hx-target, the reply goes into the button itself
Write:
<button hx-get="/order/12/status">Check status</button>
Guess first: the server replies Ironing. Where does that reply go?
Inside the button itself. htmx replaces the innerHTML of the element that sent the request, so the Check status label is gone, replaced by Ironing.
Meaning: Buttons, divs and other elements send their request on a click. Inputs, selects and textareas send it on change, forms on submit. Without hx-target, the reply replaces the element's own content.
⚡ Point at the destination with hx-target
Write:
<li id="order-12">
#12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
<button id="check-12" hx-get="/order/12/status" hx-target="#status-12">
Check status
</button>
</li>
Meaning: The value of hx-target is a CSS selector, and # means id. The reply Ironing becomes the content of #status-12, and the button isn't touched. The target has to be on the page already. If it isn't found, htmx sends no request at all.
⚡ Loading itself when the page opens
Write:
<div id="summary" hx-get="/summary" hx-trigger="load" hx-target="this">
Loading the summary...
</div>
Meaning: hx-trigger="load" sends the request once, as soon as the element is on the page. hx-target="this" points at the div itself. Don't write hx-trigger="": an empty value counts as missing, and the div goes back to waiting for a click.