Events

Htmx provides an extensive events system that can be used to modify and enhance behavior. Events are listed below.

Event - htmx:afterOnLoad

This event is triggered after an AJAX onload has finished. Note that this does not mean that the content has been swapped or settled yet, only that the request has finished.

Details

Event - htmx:afterProcessNode

This event is triggered after htmx has initialized a DOM node. It can be useful for extensions to build additional features onto a node.

Details

Event - htmx:afterRequest

This event is triggered after an AJAX request has finished either in the case of a successful request (although one that may have returned a remote error code such as a 404) or in a network error situation. This event can be paired with htmx:beforeRequest to wrap behavior around a request cycle.

Details

Event - htmx:afterSettle

This event is triggered after the DOM has settled.

Details

Event - htmx:afterSwap

This event is triggered after new content has been swapped into the DOM.

Details

Event - htmx:beforeOnLoad

This event is triggered before any response processing occurs. If the event is cancelled, no swap will occur.

Details

Event - htmx:beforeProcessNode

This event is triggered before htmx initializes a DOM node and has processed all of its hx- attributes. This gives extensions and other external code the ability to modify the contents of a DOM node before it is processed.

Details

Event - htmx:beforeRequest

This event is triggered before an AJAX request is issued. If the event is cancelled, no request will occur.

Details

Event - htmx:beforeSend

This event is triggered right before a request is sent. You may not cancel the request with this event.

Details

Event - htmx:beforeSwap

This event is triggered before any new content has been swapped into the DOM. If the event is cancelled, no swap will occur.

You can modify the default swap behavior by modifying the shouldSwap and target properties of the event detail. See the documentation on configuring swapping for more details.

Details

Event - htmx:configRequest

This event is triggered after htmx has collected parameters for inclusion in the request. It can be used to include or update the parameters that htmx will send:

document.body.addEventListener('htmx:configRequest', function(evt) {
    evt.detail.parameters['auth_token'] = getAuthToken(); // add a new parameter into the mix
});

Note that if an input value appears more than once the value in the parameters object will be an array, rather than a single value.

Details

Event - htmx:historyCacheError

This event is triggered when an attempt to save the cache to localStorage fails

Details

Event - htmx:historyCacheMiss

This event is triggered when a cache miss occurs when restoring history

Details

Event - htmx:historyCacheMissError

This event is triggered when a cache miss occurs and a response has been retrieved from the server for the content to restore, but the response is an error (e.g. 404)

Details

Event - htmx:historyCacheMissLoad

This event is triggered when a cache miss occurs and a response has been retrieved succesfully from the server for the content to restore

Details

Event - htmx:historyRestore

This event is triggered when htmx handles a history restoration action

Details

Event - htmx:beforeHistorySave

This event is triggered when htmx handles a history restoration action

Details
Details

Event - htmx:load

This event is triggered when a new node is loaded into the DOM by htmx.

Details

Event - htmx:noSSESourceError

This event is triggered when an element refers to a SSE event in its trigger, but no parent SSE source has been defined

Details

Event - htmx:onLoadError

This event is triggered when an error occurs during the load handling of an AJAX call

Details

Event - htmx:oobErrorNoTarget

This event is triggered when an out of band swap does not have a corresponding element in the DOM to switch with.

Details

Event - htmx:prompt

This event is triggered after a prompt has been shown to the user with the hx-prompt attribute. If this event is cancelled, the AJAX request will not occur.

Details

Event - htmx:pushedIntoHistory

This event is triggered after an url has been pushed into history.

Details

Event - htmx:responseError

This event is triggered when an HTTP error response occurs

Details

Event - htmx:sendError

This event is triggered when a network error prevents an HTTP request from occurring

Details

Event - htmx:sseError

This event is triggered when an error occurs with a SSE source

Details

Event - htmx:swapError

This event is triggered when an error occurs during the swap phase

Details

Event - htmx:targetError

This event is triggered when a bad selector is used for a hx-target attribute (e.g. an element id without a preceding #)

Details

Event - htmx:validation:validate

This event is triggered before an element is validated. It can be used with the elt.setCustomValidity() method to implement custom validation rules.

<form hx-post="/test">
  <input _="on htmx:validation:validate
               if my.value != 'foo'
                  call me.setCustomValidity('Please enter the value foo')
               else
                  call me.setCustomValidity('')"
         name="example">
</form>
Details

Event - htmx:validation:failed

This event is triggered when an element fails validation.

Details

Event - htmx:validation:halted

This event is triggered when a request is halted due to validation errors.

Details

Event - htmx:xhr:abort

This event is triggered when an ajax request aborts

Details

Event - htmx:xhr:loadstart

This event is triggered when an ajax request starts

Details

Event - htmx:xhr:loadend

This event is triggered when an ajax request finishes

Details

Event - htmx:xhr:progress

This event is triggered periodically when an ajax request that supports progress is in flight

Details