Mrs. Rini's /board route has been running for ages, and it answers with a full page. You add a Reload board button that calls that route, click it, and the header, the menu, even the footer turn up inside the order board. You don't want to change the route yet. All you need is its list of orders.
hx-select picks part of the HTML reply with a CSS selector, before that reply goes into the target. It's useful when the server answers with a full page, but you only need one piece of it. Like Mrs. Rini asking another branch for a photocopy of a page from their order book. What arrives is a full page: the shop's letterhead, its address, an ad, and the order table. She only needs the table, so she cuts it out and pins it on the board. The scissors are hx-select. The photocopy is still a full page: cutting doesn't make what was sent any thinner.
The problem: the server replies with a full page
Mrs. Rini's old route answers GET /board with the same page you see when it opens:
<!doctype html>
<html lang="en">
<head><title>Order board | Mrs. Rini's Laundry</title> ...</head>
<body>
<header><h1>Mrs. Rini's Laundry</h1><nav>...</nav></header>
<main id="main">
<div id="summary"><p>4 active orders, 1 ready for pickup</p></div>
<ul id="order-board">
<li id="order-12">#12 Mr. Darto, 3 kg: <span>Ironing</span></li>
...
<li id="order-15">#15 Mrs. Tini, 4 kg: <span>Queued</span></li>
</ul>
<button id="reload" ...>Reload board</button>
</main>
<footer>5 Jasmine Street</footer>
</body>
</html>
Without hx-select, htmx drops its <head>, then puts the whole <body> in the target. The header, menu, summary, button and footer show up inside the order board. The tab's title is also replaced by the reply's <title>.
hx-select: take the part you want
<button id="reload" hx-get="/board" hx-target="#order-board"
hx-select="#order-board > li">
Reload board
</button>
- htmx finds every element matching
#order-board > liinside the reply, and only those go in. Here, 4<li>. - Every match is taken, not just the first.
hx-selectreads the reply,hx-targetpoints at a place on the page. Both are CSS selectors, but they work in two different places.
The right pairs
hx-select |
hx-swap |
Result on the page |
|---|---|---|
| (none) | innerHTML |
the reply's whole <body> inside the board |
#order-board |
innerHTML |
the new <ul> inside the old <ul>: 2 elements with the id order-board |
#order-board |
outerHTML |
the old <ul> swapped for the new <ul>: 1 board, 4 orders |
#order-board > li |
innerHTML |
the board's content swapped for 4 new <li>: 1 board, 4 orders |
hx-select="#order-board" picks the whole <ul>. The default innerHTML puts it inside the old <ul>, so there are 2 elements with the id order-board, a ul inside a ul. When the part you pick is the same element as the target, use outerHTML. To keep innerHTML, pick its content: #order-board > li.
What doesn't change: the reply's size
hx-select filters after the reply arrives. In the Lab, the server still sends the full page, 961 bytes, though only the 310 byte board is used. So hx-select is a convenience, not a saving. For a truly small reply, the server reads the HX-Request: true header and answers with just the board fragment. In Laravel: if ($request->header('HX-Request')) { return view('board._order', ...); }.
hx-select-oob: take one more part
<button id="reload" hx-get="/board" hx-target="#order-board"
hx-select="#order-board" hx-swap="outerHTML"
hx-select-oob="#summary">
#summary from the same reply also replaces the #summary on the page, matched by its id. So one request updates the board and the summary at once. Out of band (OOB) swaps get a closer look in the OOB swaps lesson.
⚠️ A board that doubles, empties or disappears
- The picked element is the same as the target, with
innerHTML. You get a ul inside a ul and a doubled id. UseouterHTML, or pick its content. - A selector that matches nothing in the reply.
hx-select="#order-bord"picks zero elements, and htmx still swaps in the empty result: the board is emptied (innerHTML) or removed (outerHTML). There's no error. hx-selecton a parent element. It's inherited, so other buttons inside also filter their own replies with it, and their targets end up empty. Writehx-select="unset"on those buttons, or puthx-selectonly on the button that needs it.- Forgetting
hx-get, or a target that doesn't exist. In the exercise, the button needshx-get="/page",hx-select="#content", and anhx-targetpointing at an element that really exists,#result.
TL;DR: hx-select="#order-board" takes the matching part of the reply, every match, before it goes into the target. It's useful when the server answers with a full page. When the picked part is the same element as the target, use hx-swap="outerHTML" (or pick its content, #order-board > li), so you don't get a ul inside a ul. A selector that matches nothing empties the target with no error, and hx-select is inherited by the elements inside. hx-select-oob="#summary" takes one more part by its id. The reply's size doesn't change: for a small reply, the server answers with a fragment when HX-Request: true is there.
⚡ The server replies with a full page
Guess first: a button with no hx-select calls a route that replies with a full page. What goes into the board?
The whole content of its <body>: the header, the menu, the summary, the button, even the footer. htmx only drops its <head>.
Meaning: hx-select is useful when the server replies with a full page, but you only need one part of it.
⚡ Take the part you want
Write:
<button id="reload" hx-get="/board" hx-target="#order-board"
hx-select="#order-board > li">
Reload board
</button>
Meaning: htmx finds every element matching #order-board > li inside the reply, and only those go in. hx-select reads the reply, hx-target points at a place on the page.
⚡ Don't end up with a ul inside a ul
Meaning: hx-select="#order-board" with the default innerHTML puts the new <ul> inside the old <ul>, so 2 elements have the id order-board. When the picked part is the same element as the target, use hx-swap="outerHTML". The reply's size doesn't change, because hx-select filters after the reply arrives.