Mrs. Rini's order board has one Check status button per row, and each button has to fill the status box on its own row. If there are 40 orders today, do you have to write 40 different ids? You don't.
hx-target decides which element receives the server's HTML reply. Its value is a plain CSS selector, or a special htmx keyword that points at an element relative to the one sending the request. Mrs. Rini has two ways of telling her helper where to write a status, too. She can say "write it in order #12's status box" (an exact address, like an id), "write it in the row this button is on" (relative), or "write it in the status box right before this button" (relative too). A relative instruction works on every row without naming a number.
Plain CSS selectors
This is Mrs. Rini's order board, with one button per row:
<ul id="order-board">
<li id="order-12">
<b>#12 Mr. Darto</b>
<span id="status-12" class="status">Washing</span>
<button hx-get="/order/12/status" hx-target="#status-12">Check status</button>
</li>
<li id="order-13">
<b>#13 Ms. Wulan</b>
<span id="status-13" class="status">Ready for pickup</span>
<button hx-get="/order/13/status" hx-target="#status-13">Check status</button>
</li>
</ul>
hx-target="#status-12": an id, the clearest way. Each button points at its own status box, so each row needs its own id and value.- Any selector works:
.status,#order-board li,body. If the selector matches many elements, htmx uses the first one on the page.hx-target=".status"on button #13 changes #12's status. - The element must already be on the page when the button is clicked.
Relative keywords
These keywords are worked out from the clicked button. For the button on row #12 above:
hx-target |
The element it picks |
|---|---|
this |
the element that wrote that hx-target, here the button itself |
closest li |
<li id="order-12">: the nearest matching ancestor, starting from the button itself and going up |
previous .status |
the last .status before the button: #status-12 |
next .status |
the first .status after the button anywhere on the page: #status-13 |
previous / next |
the sibling right before / after the button (there's no next here) |
find .status |
a matching descendant of that element. The button has no such content, so nothing is found |
next .status searches forward through the whole page, not just inside the row. From button #12, the first .status after it is on row #13. So #13's status changes, though #12 was clicked. For the same row, use previous .status: the status comes before the button on its own row.
One attribute for every row
Relative keywords let every row use the same value, with no id per row:
<li id="order-12">
<b>#12 Mr. Darto</b> <span class="status">Washing</span>
<button hx-get="/order/12/status" hx-target="previous .status">Check status</button>
</li>
previous .statuson any row points at the status on that row.closest lipoints at the whole row. Careful: the reply replaces the whole<li>content, so the name and the button disappear too. Use it when the server replies with the complete row content.
hx-target is inherited
An hx-target written on a parent element applies to every descendant that has no hx-target of its own:
<ul hx-target="closest li">
<li>... <button hx-get="/order/12/card">Mark ready</button></li>
<li>... <button hx-get="/order/13/card">Mark ready</button></li>
</ul>
- Each button uses the
<ul>'sclosest li, but it's worked out from the clicked button, so each button gets its own row. - The exception: an inherited
thispoints at the element that wrote it.<div hx-target="this">makes the buttons inside it target that div.
When the target isn't found
If hx-target matches no element, htmx fires the htmx:targetError event and sends no request at all. No error shows on the page, the button just sits there. The most common cause: a missing #. hx-target="box-1" looks for an element with the tag <box-1>, not the id box-1.
⚠️ Buttons that fill another row, or nothing at all
- Forgetting the
#.hx-target="box-1"finds nothing, so the request isn't sent. In the exercise, the values must be exactly#box-1and#box-2. - Two buttons pointing at the same place. In the exercise, the
/api/content-1button fills#box-1and the/api/content-2button fills#box-2, each into its own box. nextjumping to another row.next .statussearches the whole page. For the same row, useprevious .statusorclosest li.closest liwiping the row. A one-word reply replaces the name and the button too.
TL;DR: hx-target picks the element that receives the reply. A plain CSS selector (#status-12, .status) uses the first matching element on the page, and an id must be written with #. Relative keywords are worked out from the clicked element: this (the element itself), closest li (the nearest ancestor), previous .status and next .status (searching backward or forward through the whole page), find .status (a descendant). hx-target is inherited by the elements inside. If the target isn't found, htmx sends no request.
⚡ A plain selector: point at the id
Write:
<button hx-get="/order/12/status" hx-target="#status-12">Check status</button>
Meaning: The value of hx-target is a CSS selector, and an id is written with #. If the selector matches many elements, htmx uses the first one on the page.
⚡ One attribute for every row
Write:
<li id="order-12">
<b>#12 Mr. Darto</b> <span class="status">Washing</span>
<button hx-get="/order/12/status" hx-target="previous .status">Check status</button>
</li>
Guess first: the button on row #12 uses hx-target="next .status". Whose status changes?
#13's. next .status searches forward through the whole page, not just inside the row. For the same row, use previous .status.
Meaning: Relative keywords are worked out from the clicked button: this is the element itself, closest li the nearest ancestor, previous and next search backward or forward, find looks at its descendants.
⚡ No target, no request
Guess first: the button says hx-target="box-1", with no #. What happens when it's clicked?
Nothing at all. htmx looks for a <box-1> tag, finds none, and sends no request.
Meaning: Write an id with its #, such as #box-1. An hx-target written on a parent element is also inherited by the elements inside it.