Movatterモバイル変換


[0]ホーム

URL:


</> htmx

hx-trigger

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

Standard Events

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>

Standard Event Filters

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 Event Modifiers

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

Here 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.

Non-standard Events

There are some additional non-standard events that htmx supports:

Triggering via theHX-Trigger header

If 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-eventwith 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.

Polling

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

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.

Via JavaScript

The AJAX request can be triggered via JavaScripthtmx.trigger(), too.

Notes


[8]ページ先頭

©2009-2025 Movatter.jp