EveryPage

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-oob to 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 a div that has id="message" and hx-swap-oob="true" swaps directly into the existing #message element, while the rest of the response goes to the target as usual.

    The hx-swap-oob value controls how the swap happens:

    • true or outerHTML: 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 except outerHTML.
    • 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-oob takes a comma-separated list of IDs to pick out of the response and swap out of band. It is almost always paired with hx-select. Each item defaults to outerHTML, 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 is innerHTML. Some limits apply: the hx-swap value must not contain spaces; the delete method requires the response to include the deleted element, which can be an empty div with that id; 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-indicator class 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 the htmx-request class 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-request is added to the requesting element by default, you can place the indicator inside it without an hx-indicator attribute.
    • Elsewhere on the page: Use hx-indicator with a CSS selector to name the element that receives the htmx-request class, such as hx-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 inherit keyword in hx-indicator to keep a parent's indicator selectors and add more, for example hx-indicator="inherit, #spinner". The closest keyword 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 display on a custom class such as my-indicator under .htmx-request.

    You can also disable elements for the duration of a request by adding the hx-disabled-elt attribute.

    Answered from hx-indicator, Documentation — Request Indicators

  • How do I send a request a moment after the user stops typing?

    Use the delay modifier on the hx-trigger attribute, for example hx-trigger="keyup changed delay:500ms". A delay holds 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 give 1s and 500ms as example intervals.

    Add changed to the trigger so the request is sent only when the input's value has actually changed. The records note that change is the event name and changed is the modifier, so the two are not interchangeable.

    A complete search input built this way uses hx-post with hx-trigger="input changed delay:500ms" and a target element for the results.

    Two related modifiers behave differently. throttle instead 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 the queue option.

    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