hx-trigger

The hx-trigger attribute allows you to specify what triggers an AJAX request. A trigger value can be one of the following:

Standard Events

A standard event, such as click can be specified as the trigger like so:

<div hx-get="/clicked" hx-trigger="click">Click Me</div>

Standard events can also have modifiers that change how they behave. The modifiers are:

Here is an example of a search box that searches on keyup, but only if the search value has changed and the user hasn't typed anything new for 1 second:

<input name="q" 
       hx-get="/search" hx-trigger="keyup changed delay:1s"
       hx-target="#search-results"/>

The response from the /register url will be appended to the div with the id response-div.

There are two special events that are non-standard that htmx supports:

Polling

By using the syntax every <timing declaration> you can have an element poll periodically:

<div hx-get="/latest_updates" hx-trigger="every 1s">
  Nothing Yet!
</div>

This example will issue a GET to the /latest_updates URL every second and swap the results into the innerHTML of this div.

Multiple Triggers

Multiple triggers can be provided, seprarated by commas. Each trigger gets its own options.

  <div hx-get="/news" hx-trigger="load, click delay:1s"></div>

This example will load /news immediate on the page load, and then again with a delay of one second after each click.

Notes