Unofficial example
Ask the htmx docs
The Ask this site box, reading htmx.org. Type a question and it answers from those pages, with links.
Loading the box…
An unofficial example. Ask this site is not affiliated with or endorsed by the htmx project. The htmx docs belong to their authors and are read here under their 0BSD licence.
Questions it answered
Real answers from this box.
How do I swap content into a different element?
You can swap content into a different element by sending it out of band. Add
hx-swap-oobto an element in the server response, and htmx swaps it into the DOM by matching its id, not into the request's target. For example, a response with adivthat hasid="message"andhx-swap-oob="true"swaps directly into the existing#messageelement, while the rest of the response goes to the target as usual.The
hx-swap-oobvalue controls how the swap happens:trueorouterHTML: the element is swapped inline, and the two are equivalent.- A swap value such as
beforeend: that strategy is used, and the wrapping tag is stripped for every strategy exceptouterHTML. - A swap value followed by a colon and a CSS selector: every element matching the selector is swapped.
- No selector: the element whose ID matches the new content is swapped.
On the requesting element,
hx-select-oobtakes a comma-separated list of IDs to pick out of the response and swap out of band. It is almost always paired withhx-select. Each item defaults toouterHTML, but you can add a strategy after a colon, such as#alert:afterbegin.To update several elements from one request, the multi-swap extension accepts
hx-swap="multi:#id1,#id2", with an optional swap method after each selector. The default method isinnerHTML. Some limits apply: thehx-swapvalue must not contain spaces; thedeletemethod requires the response to include the deleted element, which can be an empty div with thatid; and only ID selectors are supported.Answered from hx-swap-oob, Documentation — Out of Band Swaps, hx-select-oob, Documentation — Selecting Content To Swap, README.md (cont. 20)
How do I show a loading indicator during a request?
To show a loading indicator, add the
htmx-indicatorclass to an element such as a spinner image. It is invisible by default, and htmx reveals it while the request is in flight. htmx adds thehtmx-requestclass to the requesting element for the duration of the request, and that class makes the indicator visible.The default CSS fades the indicator in with opacity and sets visibility to hidden when it is not shown, which also helps screen readers.
Where the indicator sits determines how you wire it up:
- Inside the requesting element: Because
htmx-requestis added to the requesting element by default, you can place the indicator inside it without anhx-indicatorattribute. - Elsewhere on the page: Use
hx-indicatorwith a CSS selector to name the element that receives thehtmx-requestclass, such ashx-indicator="#spinner". The target can also be an ancestor of the indicator, because the indicator only needs to be in that element's hierarchy. - Inherited from a parent: Use the
inheritkeyword inhx-indicatorto keep a parent's indicator selectors and add more, for examplehx-indicator="inherit, #spinner". Theclosestkeyword followed by a selector targets the nearest matching ancestor.
If you prefer a different effect than fading, you can write your own CSS instead of the opacity transition. For example, you can toggle
displayon a custom class such asmy-indicatorunder.htmx-request.You can also disable elements for the duration of a request by adding the
hx-disabled-eltattribute.Answered from hx-indicator, Documentation — Request Indicators
- Inside the requesting element: Because
How do I send a request a moment after the user stops typing?
Use the
delaymodifier on thehx-triggerattribute, for examplehx-trigger="keyup changed delay:500ms". Adelayholds the request until a pause in the events, and each new event resets the countdown, so the request fires only after the user stops typing. The records give1sand500msas example intervals.Add
changedto the trigger so the request is sent only when the input's value has actually changed. The records note thatchangeis the event name andchangedis the modifier, so the two are not interchangeable.A complete search input built this way uses
hx-postwithhx-trigger="input changed delay:500ms"and a target element for the results.Two related modifiers behave differently.
throttleinstead waits the given time after the first event, discards events that arrive in the meantime, and fires at the end of the period. If an event occurs while another request is in flight, the default queue behaviour is to queue the last event, and you can change this with thequeueoption.Answered from hx-trigger — Standard Event Modifiers, Documentation — Trigger Modifiers, Active Search
Add this to your own docs
Paste your docs address to try it, free and without signing up. Then it is one line of code.
DocusaurusGitBookMintlifyWordPressReadMeMkDocsVitePressStarlightSphinxHugoJekyllNext.jsGhostWebflow