Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 28 ยท HTMX in Practice

Common Interaction Patterns

Build the interactions every site needs with HTMX: live search, click to edit, inline delete, load more, infinite scroll, lazy loading, polling, tabs and bulk actions.

Once you know requests, triggers, targets and swaps, a surprising number of interface features turn out to be the same few moves in different clothes. A live search box is "send on input, swap the results". Infinite scroll is "send when an element becomes visible, swap in the next page". Inline editing is "swap a display view for a form, then swap it back".

This lesson is a cookbook. Each pattern is short, uses only attributes you have met, and runs in the page. Keep it as a reference for the day you need one of them. The server side of each pattern is only a few lines in the mock server; in a real project those would be ordinary routes returning partial templates.

Patterns in this lesson

Live search Debounced input, results swap in.
Click to edit Display view and form trade places.
Inline delete Confirm in place, then remove the row.
Paging Load more or infinite scroll.
1 / 2

Live search and click to edit

Live search. Send the query when the user types, but wait until they pause. hx-trigger="input changed delay:300ms" fires 300 ms after the last keystroke, and only if the value really changed. Adding search as a second trigger (comma separated) also fires when the user presses Enter or clicks the clear button in a search field. Pair it with hx-sync="this:replace" so a slow older request can never overwrite a newer one (see Loading States).

Click to edit. The display view and the form are two fragments of the same element. The display view has an Edit button with hx-get to fetch the form; the form's hx-put saves and returns the display view again. Both use hx-target="this" and hx-swap="outerHTML" on the wrapper, so the wrapper swaps itself.

Live search and click to edit

Type py and pause: only one request goes out. Click Edit, change the name, and press Save. The form carries the same id as the display view so the swap target stays consistent. The server returns view() in three places, which is a hint that this should be one shared partial template in a real project.

Inline delete and load more

Inline delete. Native hx-confirm shows a browser dialog, which is perfect for most sites. A friendlier pattern asks inside the row: the Delete button fetches a "Sure?" fragment, and "Yes" sends the DELETE request, which returns an empty 200 response that removes the row. "No" fetches the original row back.

Load more. The button itself is replaced by the next page of items, followed by a new button for the page after that. When there are no more pages, the server simply omits the button. Because the button swaps itself with hx-swap="outerHTML", no id bookkeeping is needed.

Inline delete and load more

The delete rows swap themselves: hx-target="this" and hx-swap="outerHTML" are on the li, and every button inside inherits them. In the load-more example the button lives in the last li. The response contains the next items followed by a fresh li with the next button, and that whole response replaces the old button item. On page 4 the server leaves the button out, so the list simply ends. Letting the server decide what replaces the control is what makes the end-of-list state automatic.

Infinite scroll, lazy loading and polling

Infinite scroll uses the revealed trigger, which fires once when an element scrolls into view. The last element of every page is a sentinel that asks for the next page and replaces itself with the new items plus a new sentinel.

Lazy loading is the same idea with hx-trigger="load": a cheap placeholder in the first response, and the expensive panel fetched right after. (intersect once does it only when the panel scrolls into view.)

Polling is hx-trigger="every 2s". The element requests itself repeatedly. A server can stop the polling by answering with status 286.

Infinite scroll, lazy panel and polling

The feed grows as you scroll to the sentinel and ends after page 4. The stats panel loads itself 800 ms after the page appears. The polling panel counts up each second and then stops, because status 286 tells HTMX to cancel the poll.

Polling costs a request per client per interval. For dashboards that is fine at modest scale; for thousands of watchers prefer server-sent events or a longer interval. Stopping polls when the work is done (as the 286 trick does) saves a lot of traffic.

Tabs and bulk actions

Tabs loaded as fragments. Each tab button requests its own panel. The simplest robust approach is to return the entire tab widget each time, with the right tab marked selected, and swap the widget with outerHTML. The server owns the "which tab is active" decision, and the markup stays accessible (role="tablist", aria-selected).

Bulk updates. A form of checkboxes needs no JavaScript at all. Buttons inside the form issue hx-post requests, and because they sit inside a form, HTMX includes the checked boxes' values automatically. The server reads all id values, updates those rows and returns the updated table body.

Tabs and bulk actions

Check "Grace" and "Linus", press "Activate selected", and both rows update in a single request. The bulk buttons live inside the form, and hx-post on a button inside a form includes the form's values. The form's hx-target and hx-swap are inherited by both buttons.

Every pattern in this lesson works without any custom JavaScript, but each also has a plain-HTML ancestor: a link to the next page, a form that posts, a page per tab. Building the plain version first and layering HTMX on top (progressive enhancement) is the most resilient way to work. The history lesson shows how.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does hx-trigger="input changed delay:300ms" do on a text input?

    delay debounces the event, and changed skips requests when the value is the same as the previous one.
  2. 2. In the click-to-edit pattern, which attribute pair lets the wrapper replace itself with the form?

    The element is the target and outerHTML replaces the element itself, so the display view and the form trade places.
  3. 3. Which trigger loads the next page when a sentinel element scrolls into view?

    revealed fires once when the element enters the viewport, which is the basis of infinite scroll.
  4. 4. How can a server stop an element that polls with hx-trigger="every 2s"?

    HTMX treats the special status 286 as a signal to cancel polling for that element.
  5. 5. A bulk-action button sits inside a form of checkboxes. What does it send with hx-post?

    For non-GET requests, an element inside a form also submits that form's values, including checked checkboxes.

Summary

  • Live search: hx-trigger="input changed delay:300ms, search" plus hx-sync="this:replace".
  • Click to edit: two fragments that swap each other with hx-target="this" and hx-swap="outerHTML".
  • Inline delete: fetch a confirmation fragment, then hx-delete returns an empty 200; hx-confirm is the native alternative.
  • Paging: a "load more" button or a revealed sentinel that replaces itself with the next page and the next control.
  • load loads panels lazily, every Ns polls, and status 286 stops polling.
  • Tabs and bulk actions are plain fragments and plain forms: the server owns the state.

Continue with Events, Headers and JavaScript Integration.