liveBook · Manning

Hx-trigger attribute

As explained in Django in Action

hx-trigger is an HTMX HTML attribute that specifies the browser event and timing conditions that initiate an HTMX request.

hx-trigger tells HTMX when to run the request named by an attribute such as hx-get. Put it on the element involved in the interaction, then choose an event: load for page loading, keyup for typing, or revealed when an element scrolls into view. The request can return a partial HTML response while hx-target and hx-swap decide where and how that response changes the page. section 12.2 section 12.7

Common trigger patterns include:

  • Use hx-trigger="load" with hx-get on a placeholder to fetch slow content after the page appears. The placeholder remains visible until the response replaces it. section 12.3
  • Use hx-trigger="keyup changed delay:500ms" on a search input to request results only after a key release changes the value and typing pauses for 500 milliseconds. section 12.4
  • Use hx-trigger="revealed" on a loading element to fetch another page of results when that element becomes visible during scrolling. section 12.4

Delay a promoter list until after page load

A promoter page can start with a friendly “Loading promoters...” placeholder. Give its container an hx-get URL and hx-trigger="load". Once the page loads, HTMX calls the Django view; that view renders only the promoter-list partial, which replaces the placeholder. The main page stays responsive even when the partial takes time to generate. section 12.3

Keep search requests from becoming noisy

A musician search input can send its value through hx-get on keyup, target a results container, and push the query into the URL. Adding changed prevents arrow-key releases from requesting unchanged text; delay:500ms waits for a pause, giving both the server and frantic typists a small break. section 12.4

Figure 12.1 Using HTMX attributes to change a link into an action that replaces a portion of the page
figure

See the basic HTMX flow: an interaction invokes a request and replaces part of the page with the response. section 12.2

How Django in Action covers it

How deep it goes
Explains concrete trigger values and filters through lazy loading, search, and infinite-scroll implementations rather than HTMX internals.
Examples
Demonstrates a delayed promoter listing, debounced musician search, and revealed-element pagination for infinite scroll.
What it assumes
Builds on Django views, URL routes, templates, and HTTP GET requests while introducing HTMX-triggered partial responses.
What you can do after
Apply it via HTMX to time GET requests for loading content, searching input, and fetching additional scrolling results.
How it connects
Connects hx-get, hx-target, hx-swap, partials, lazy loading, search-as-you-type, infinite scroll, and Django views.

Also explained in

Compare how 2 books explain hx-trigger attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.