liveBook · Manning

Hx-trigger attribute

As explained in Quarkus in Action

The hx-trigger attribute declares the events that cause HTMX to issue a request or refresh an element.

Use `hx-trigger` to name the events that make an HTMX element issue its request. An availability table can load automatically when the page opens and refresh when a reservation signals that the list has changed. section 6.2.3

Listing 6.13 Including the list of available cars in the index page
<h2>Available cars to rent</h2>
<form hx-get="/available" hx-target="#availability">
  <p>Start date:<input id="startDateInput" type="date"
                       name="startDate" value="{startDate}"/></p>
  <p>End date:<input id="endDateInput" type="date"
                     name="endDate" value="{endDate}"/></p>
  <input type="submit" value="Update list"/>
</form>
<div id="availability" hx-get="/available"  #1
     hx-trigger="load, update-available-cars-list from:body"
     hx-include="[id='startDateInput'],[id='endDateInput']">
  <!-- To be replaced by the result of calling /available -->
</div>

The availability div uses `hx-get` for the request and `hx-trigger="load, update-available-cars-list from:body"` for its two events. The `from:body` modifier is required for the custom event here; `hx-include` sends the current date inputs. section 6.2.3

The date form takes a separate route: submitting it requests `/available` and uses `hx-target` to replace the availability div. Its submission does not need another event in the div’s `hx-trigger` list. section 6.2.3

How Quarkus in Action covers it

How deep it goes
Works through load and a custom body event on an availability div, without cataloging other triggers.
Examples
Shows tables loading on page entry and an availability list listening for a reservation-driven refresh event.
What you can do after
Apply it via HTMX to load tables on page entry and listen for a custom refresh event.
How it connects
Links event triggering to hx-get, hx-include, hx-target, Qute templates, and REST endpoints.

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 Quarkus in Action. The section links point to the parts of the book it draws on.