hx-trigger
Thehx-trigger
attribute allows you to specify what triggers an AJAX request. A triggervalue can be one of the following:
every <timing declaration>
Standard events refer toweb API events (e.g. click, keydown, mouseup, load).
A standard event, such asclick
can be specified as the trigger like so:
<divhx-get="/clicked"hx-trigger="click">Click Me</div>
Events can be filtered by enclosing a boolean javascript expression in square brackets after the event name. Ifthis expression evaluates totrue
the event will be triggered, otherwise it will be ignored. Standard event filtersrequire eval.
<divhx-get="/clicked"hx-trigger="click[ctrlKey]">Control Click Me</div>
This event will trigger if a click event is triggered with theevent.ctrlKey
property set to true.
Conditions can also refer to global functions or state
<divhx-get="/clicked"hx-trigger="click[checkGlobalState()]">Control Click Me</div>
And can also be combined using the standard javascript syntax
<divhx-get="/clicked"hx-trigger="click[ctrlKey&&shiftKey]">Control-Shift Click Me</div>
Note that all symbols used in the expression will be resolved first against the triggering event, and then nextagainst the global namespace, somyEvent[foo]
will first look for a property namedfoo
on the event, then lookfor a global symbol with the namefoo
Standard events can also have modifiers that change how they behave. The modifiers are:
once
- the event will only trigger once (e.g. the first click)changed
- the event will only fire if the value of the element has changed. Please pay attentionchange
is the name of the event andchanged
is the name of the modifier.delay:<timing declaration>
- a delay will occur before an event triggers a request. If the eventis seen again it will reset the delay.throttle:<timing declaration>
- a throttle will occur after an event triggers a request. If the eventis seen again before the delay completes, it is ignored, the element will trigger at the end of the delay.from:<Extended CSS selector>
- allows the event that triggers a request to come from another element in the document (e.g. listening to a key event on the body, to support hot keys)from:input
would listen on every input on the page.hx-trigger="click[event.target.matches('button')] from:body"
which wouldcatch click events from every button on the page.document
- listen for events on the documentwindow
- listen for events on the windowclosest <CSS selector>
- finds theclosest ancestor element or itself, matching the given css selectorfind <CSS selector>
- finds the closest child matching the given css selectornext
resolves toelement.nextElementSiblingnext <CSS selector>
scans the DOM forward for the first element that matches the given CSS selector.(e.g.next .error
will target the closest following sibling element witherror
class)previous
resolves toelement.previousElementSiblingprevious <CSS selector>
scans the DOM backwards for the first element that matches the given CSS selector.(e.g.previous .error
will target the closest previous sibling witherror
class)target:<CSS selector>
- allows you to filter via a CSS selector on the target of the event. This can be useful when you want to listen fortriggers from elements that might not be in the DOM at the point of initialization, by, for example, listening on the body,but with a target filter for a child elementconsume
- if this option is included the event will not trigger any other htmx requests on parents (or on elementslistening on parents)queue:<queue option>
- determines how events are queued if an event occurs while a request for another event is in flight. Options are:first
- queue the first eventlast
- queue the last event (default)all
- queue all events (issue a request for each event)none
- do not queue new eventsHere is an example of a search box that searches onkeyup
, but only if the search value has changedand the user hasn’t typed anything new for 1 second:
<inputname="q"hx-get="/search"hx-trigger="keyup changed delay:1s"hx-target="#search-results"/>
The response from the/search
url will be appended to thediv
with the idsearch-results
.
There are some additional non-standard events that htmx supports:
load
- triggered on load (useful for lazy-loading something)revealed
- triggered when an element is scrolled into the viewport (also useful for lazy-loading). If you are usingoverflow
in css likeoverflow-y: scroll
you should useintersect once
instead ofrevealed
.intersect
- fires once when an element first intersects the viewport. This supports two additional options:root:<selector>
- a CSS selector of the root element for intersectionthreshold:<float>
- a floating point number between 0.0 and 1.0, indicating what amount of intersection to fire the event onHX-Trigger
headerIf you’re trying to fire an event fromHX-Trigger
response header, you will likely want touse thefrom:body
modifier. E.g. if you send a header like thisHX-Trigger: my-custom-event
with a response, an element would likely need to look like this:
<divhx-get="/example"hx-trigger="my-custom-event from:body"> Triggered by HX-Trigger header... </div>
in order to fire.
This is because the header will likely trigger the event in a different DOM hierarchy than the element that youwish to be triggered. For a similar reason, you will often listen for hot keys from the body.
By using the syntaxevery <timing declaration>
you can have an element poll periodically:
<divhx-get="/latest_updates"hx-trigger="every 1s"> Nothing Yet!</div>
This example will issue aGET
to the/latest_updates
URL every second and swap the results intothe innerHTML of this div.
If you want to add a filter to polling, it should be addedafter the poll declaration:
<divhx-get="/latest_updates"hx-trigger="every 1s [someConditional]"> Nothing Yet!</div>
Multiple triggers can be provided, separated by commas. Each trigger gets its own options.
<divhx-get="/news"hx-trigger="load, click delay:1s"></div>
This example will load/news
immediately on page load, and then again with a delay of one second after each click.
The AJAX request can be triggered via JavaScripthtmx.trigger()
, too.
hx-trigger
is not inheritedhx-trigger
can be used without an AJAX request, in which case it will only fire thehtmx:trigger
eventform input
) to thefrom
- ortarget
-modifier, surround the selector in parentheses or curly brackets (e.g.from:(form input)
orfrom:closest (form input)
)