Htmx provides an extensive events system that can be used to modify and enhance behavior. Events are listed below.
htmx:abortThis event is different than other events: htmx does not trigger it, but rather listens for it.
If you send an htmx:abort event to an element making a request, it will abort the request:
<button id="request-button" hx-post="/example">Issue Request</button>
<button onclick="htmx.trigger('#request-button', 'htmx:abort')">Cancel Request</button>
htmx:afterOnLoadThis 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.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:afterProcessNodeThis event is triggered after htmx has initialized a DOM node. It can be useful for extensions to build additional features onto a node.
detail.elt - the element that dispatched the requesthtmx:afterRequestThis 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.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requestdetail.successful - true if the response has a 20x status code or is marked detail.isError = false in the
htmx:beforeSwap event, else falsedetail.failed - true if the response does not have a 20x status code or is marked detail.isError = true in the
htmx:beforeSwap event, else falsehtmx:afterSettleThis event is triggered after the DOM has settled.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:afterSwapThis event is triggered after new content has been swapped into the DOM.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:beforeCleanupElementThis event is triggered before htmx disables an element or removes it from the DOM.
detail.elt - the cleaned up elementhtmx:beforeOnLoadThis event is triggered before any response processing occurs. If the event is cancelled, no swap will occur.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:beforeProcessNodeThis 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.
detail.elt - the element that dispatched the requesthtmx:beforeRequestThis event is triggered before an AJAX request is issued. If the event is cancelled, no request will occur.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:beforeSendThis event is triggered right before a request is sent. You may not cancel the request with this event.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:beforeSwapThis 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.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.requestConfig - the configuration of the AJAX requestdetail.shouldSwap - if the content will be swapped (defaults to false for non-200 response codes)detail.ignoreTitle - if true any title tag in the response will be ignoreddetail.target - the target of the swaphtmx:beforeTransitionThis event is triggered before a View Transition wrapped swap occurs. If the event is cancelled, the View Transition will not occur and the normal swapping logic will happen instead.
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.requestConfig - the configuration of the AJAX requestdetail.shouldSwap - if the content will be swapped (defaults to false for non-200 response codes)detail.target - the target of the swaphtmx:configRequestThis 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.
detail.parameters - the parameters that will be submitted in the requestdetail.unfilteredParameters - the parameters that were found before filtering by hx-selectdetail.headers - the request headersdetail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.verb - the HTTP verb in usehtmx:confirmThis event is triggered immediately after a trigger occurs on an element. It allows you to cancel (or delay) issuing
the AJAX request. If you call preventDefault() on the event, it will not issue the given request. The detail
object contains a function, evt.detail.issueRequest(), that can be used to issue the actual AJAX request at a
later point. Combining these two features allows you to create an asynchronous confirmation dialog.
Here is an example using sweet alert:
document.body.addEventListener('htmx:confirm', function(evt) {
evt.preventDefault();
swal({
title: "Are you sure?",
text: "Are you sure you are sure?",
icon: "warning",
buttons: true,
dangerMode: true,
}).then((confirmed) => {
if (confirmed) {
evt.detail.issueRequest();
}
});
});
{target: target, elt: elt, path: path, verb: verb, triggeringEvent: event, etc: etc, issueRequest: issueRequest}
detail.elt - the element in questiondetail.etc - additional request information (mostly unused)detail.issueRequest - a no argument function that can be invoked to issue the request (should be paired with evt.preventDefault()!)detail.path - the path of the requestdetail.target - the target of the requestdetail.triggeringEvent - the original event that triggered this requestdetail.verb - the verb of the request (e.g. GET)htmx:historyCacheErrorThis event is triggered when an attempt to save the cache to localStorage fails
detail.cause - the Exception that was thrown when attempting to save history to localStoragehtmx:historyCacheMissThis event is triggered when a cache miss occurs when restoring history
detail.xhr - the XMLHttpRequest that will retrieve the remote content for restorationdetail.path - the path and query of the page being restoredhtmx:historyCacheMissErrorThis 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)
detail.xhr - the XMLHttpRequestdetail.path - the path and query of the page being restoredhtmx:historyCacheMissLoadThis event is triggered when a cache miss occurs and a response has been retrieved successfully from the server for the content to restore
detail.xhr - the XMLHttpRequestdetail.path - the path and query of the page being restoredhtmx:historyRestoreThis event is triggered when htmx handles a history restoration action
detail.path - the path and query of the page being restoredhtmx:beforeHistorySaveThis event is triggered when htmx handles a history restoration action
detail.path - the path and query of the page being restoreddetail.historyElt - the history element being restored intodetail.config - the config that will be passed to the EventSource constructorhtmx:loadThis event is triggered when a new node is loaded into the DOM by htmx.
detail.elt - the newly added elementhtmx:noSSESourceErrorThis event is triggered when an element refers to an SSE event in its trigger, but no parent SSE source has been defined
detail.elt - the element with the bad SSE triggerhtmx:oobAfterSwapThis event is triggered as part of an out of band swap and behaves identically to an after swap event
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:oobBeforeSwapThis event is triggered as part of an out of band swap and behaves identically to a before swap event
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.requestConfig - the configuration of the AJAX requestdetail.shouldSwap - if the content will be swapped (defaults to false for non-200 response codes)detail.target - the target of the swaphtmx:oobErrorNoTargetThis event is triggered when an out of band swap does not have a corresponding element in the DOM to switch with.
detail.content - the element with the bad oob idhtmx:onLoadErrorThis event is triggered when an error occurs during the load handling of an AJAX call
detail.xhr - the XMLHttpRequestdetail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.exception - the exception that occurreddetail.requestConfig - the configuration of the AJAX requesthtmx:promptThis 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.
detail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.prompt - the user response to the prompthtmx:beforeHistoryUpdateThis event is triggered before a history update is performed. It can be
used to modify the path or type used to update the history.
detail.history - the path and type (push, replace) for the history updatedetail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:pushedIntoHistoryThis event is triggered after a URL has been pushed into history.
detail.path - the path and query of the URL that has been pushed into historyhtmx:replacedInHistoryThis event is triggered after a URL has been replaced in history.
detail.path - the path and query of the URL that has been replaced in historyhtmx:responseErrorThis event is triggered when an HTTP error response occurs
detail.xhr - the XMLHttpRequestdetail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:sendErrorThis event is triggered when a network error prevents an HTTP request from occurring
detail.xhr - the XMLHttpRequestdetail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:sseErrorThis event is triggered when an error occurs with an SSE source
detail.elt - the element with the bad SSE sourcedetail.error - the errordetail.source - the SSE sourcehtmx:swapErrorThis event is triggered when an error occurs during the swap phase
detail.xhr - the XMLHttpRequestdetail.elt - the element that triggered the requestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:targetErrorThis event is triggered when a bad selector is used for a hx-target attribute (e.g. an
element ID without a preceding #)
detail.elt - the element that triggered the requestdetail.target - the bad CSS selectorhtmx:timeoutThis event is triggered when a request timeout occurs. This wraps the typical timeout event of XMLHttpRequest.
Timeout time can be set using htmx.config.timeout or per element using hx-request
detail.elt - the element that dispatched the requestdetail.xhr - the XMLHttpRequestdetail.target - the target of the requestdetail.requestConfig - the configuration of the AJAX requesthtmx:triggerThis event is triggered whenever an AJAX request would be, even if no AJAX request is specified. It
is primarily intended to allow hx-trigger to execute client-side scripts; AJAX requests have more
granular events available, like htmx:beforeRequest or htmx:afterRequest.
detail.elt - the element that triggered the requesthtmx:validateUrlThis event is triggered before a request is made, allowing you to validate the URL that htmx is going to request. If
preventDefault() is invoked on the event, the request will not be made.
document.body.addEventListener('htmx:validateUrl', function (evt) {
// only allow requests to the current server as well as myserver.com
if (!evt.detail.sameHost && evt.detail.url.hostname !== "myserver.com") {
evt.preventDefault();
}
});
detail.elt - the element that triggered the requestdetail.url - the URL Object representing the URL that a request will be sent to.detail.sameHost - will be true if the request is to the same host as the documenthtmx:validation:validateThis 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>
detail.elt - the element that triggered the requesthtmx:validation:failedThis event is triggered when an element fails validation.
detail.elt - the element that triggered the requestdetail.message - the validation error messagedetail.validity - the validity object, which contains properties specifying how validation failedhtmx:validation:haltedThis event is triggered when a request is halted due to validation errors.
detail.elt - the element that triggered the requestdetail.errors - an array of error objects with the invalid elements and errors associated with themhtmx:xhr:abortThis event is triggered when an ajax request aborts
detail.elt - the element that triggered the requesthtmx:xhr:loadstartThis event is triggered when an ajax request starts
detail.elt - the element that triggered the requesthtmx:xhr:loadendThis event is triggered when an ajax request finishes
detail.elt - the element that triggered the requesthtmx:xhr:progressThis event is triggered periodically when an ajax request that supports progress is in flight
detail.elt - the element that triggered the request