</> htmx
🚧 htmx 4.0 is under construction. Read changes →

Reference

Core Attributes

The most common attributes when using htmx.

AttributeDescription
hx-getissues a GET to the specified URL
hx-postissues a POST to the specified URL
hx-on*handle events with inline scripts on elements
hx-push-urlpush a URL into the browser location bar to create history
hx-selectselect content to swap in from a response
hx-swapcontrols how content will swap in (outerHTML, innerHTML, beforeend, afterend, …)
hx-swap-oobmark element to swap in from a response (out of band)
hx-targetspecifies the target element to be swapped
hx-triggerspecifies the event that triggers the request

Additional Attributes

All other attributes available in htmx.

AttributeDescription
hx-actionspecifies the URL to issue the request to
hx-boostadd progressive enhancement for links and forms
hx-configconfigure request behavior with JSON
hx-confirmshows a confirm() dialog or runs async confirmation before issuing a request
hx-deleteissues a DELETE to the specified URL
hx-disableadds the disabled attribute to the specified elements while a request is in flight
hx-encodingchanges the request encoding type
hx-headersadds to the headers that will be submitted with the request
hx-ignoredisables htmx processing for the given node and any children nodes
hx-includeinclude additional data in requests
hx-indicatorthe element to put the htmx-request class on during the request
hx-methodspecifies the HTTP method to use
hx-optimisticshow optimistic content while request is in flight
hx-patchissues a PATCH to the specified URL
hx-preloadpreload a request on a trigger event
hx-preservespecifies elements to keep unchanged between requests
hx-putissues a PUT to the specified URL
hx-replace-urlreplace the URL in the browser location bar
hx-synccontrol how requests made by different elements are synchronized
hx-validateforce elements to validate themselves before a request
hx-valsadd values dynamically to the parameters that will be submitted with the request

CSS Classes

ClassDescription
htmx-addedApplied to a new piece of content before it is swapped, removed after it is settled.
htmx-indicatorA dynamically generated class that will toggle visible (opacity:1) when a htmx-request class is present
htmx-requestApplied to either the element or the element specified with hx-indicator while a request is ongoing
htmx-settlingApplied to a target after content is swapped, removed after it is settled. The duration can be modified via hx-swap.
htmx-swappingApplied to a target before any content is swapped, removed after it is swapped. The duration can be modified via hx-swap.

HTTP Headers

Request Headers

HeaderDescription
HX-Boostedindicates that the request is via an element using hx-boost
HX-Current-URLthe current URL of the browser
HX-History-Restore-Request“true” if the request is for history restoration
HX-Requestalways “true” for htmx-initiated requests
HX-Request-Type“partial” for targeted swaps, “full” when targeting body or using hx-select
HX-Sourceidentifier of the triggering element in format tag#id (e.g., button#submit)
HX-Targetidentifier of the target element in format tag#id (e.g., div#results)

Response Headers

HeaderDescription
HX-Locationallows you to do a client-side redirect that does not do a full page reload
HX-Push-Urlpushes a new url into the history stack
HX-Redirectcan be used to do a client-side redirect to a new location
HX-Refreshif set to “true” the client-side will do a full refresh of the page
HX-Replace-Urlreplaces the current URL in the location bar
HX-Reswapallows you to specify how the response will be swapped. See hx-swap for possible values
HX-Retargeta CSS selector that updates the target of the content update to a different element on the page
HX-Reselecta CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing hx-select on the triggering element
HX-Triggerallows you to trigger client-side events
HX-Trigger-After-Settleallows you to trigger client-side events after the settle step
HX-Trigger-After-Swapallows you to trigger client-side events after the swap step

Events

Note: htmx 4 uses a new event naming convention with the pattern htmx:phase:action (e.g., htmx:before:request, htmx:after:swap).

EventDescription
htmx:abortsend this event to an element to abort a request
htmx:before:inittriggered before htmx initializes a node
htmx:after:inittriggered after htmx has initialized a node
htmx:before:cleanuptriggered before htmx cleans up an element
htmx:after:cleanuptriggered after htmx has cleaned up an element
htmx:config:requesttriggered before the request, allows you to customize parameters, headers
htmx:before:requesttriggered before an AJAX request is made
htmx:after:requesttriggered after an AJAX request has completed
htmx:finally:requesttriggered in the finally block after a request
htmx:before:swaptriggered before a swap is done, allows you to configure the swap
htmx:after:swaptriggered after new content has been swapped in
htmx:before:restore:historytriggered before history restoration
htmx:before:history:updatetriggered before history is updated
htmx:after:history:updatetriggered after history has been updated
htmx:after:push:into:historytriggered after a url is pushed into history
htmx:after:replace:into:historytriggered after a url is replaced in history
htmx:errortriggered when an error occurs (network, response, swap, etc.)
htmx:confirmtriggered after a trigger occurs on an element, allows you to cancel (or delay) issuing the AJAX request
htmx:before:viewTransitiontriggered before a view transition
htmx:after:viewTransitiontriggered after a view transition
htmx:before:sse:streamtriggered before an SSE stream is processed
htmx:after:sse:streamtriggered after an SSE stream ends
htmx:before:sse:messagetriggered before an SSE message is processed
htmx:after:sse:messagetriggered after an SSE message is processed
htmx:before:sse:reconnecttriggered before reconnecting to an SSE stream

JavaScript API

MethodDescription
htmx.ajax()Issues an htmx-style ajax request
htmx.configA property that holds the current htmx config object
htmx.registerExtension()Registers an htmx extension
htmx.find()Finds a single element matching the selector (supports extended selectors)
htmx.findAll()Finds all elements matching a given selector (supports extended selectors)
htmx.forEvent()Returns a promise that resolves when the specified event fires
htmx.on()Creates an event listener on the given element or document
htmx.onLoad()Adds a callback handler for the htmx:after:init event
htmx.parseInterval()Parses an interval declaration into a millisecond value
htmx.process()Processes the given element and its children, hooking up any htmx behavior
htmx.swap()Performs swapping of HTML content
htmx.takeClass()Takes a class from other elements for the given element
htmx.timeout()Returns a promise that resolves after the specified time
htmx.trigger()Triggers an event on an element

Configuration

Htmx has some configuration options that can be accessed either programmatically or declaratively. They are listed below:

Config VariableInfo
htmx.config.logAlldefaults to false, if set to true htmx will log all events to the console for debugging
htmx.config.prefixdefaults to "" (empty string), allows you to use a custom prefix for htmx attributes (e.g., "data-hx-" to use data-hx-get instead of hx-get)
htmx.config.transitionsdefaults to false, whether to use view transitions when swapping content (if browser supports it)
htmx.config.historydefaults to true, whether to enable history support (push/replace URL). Set to "reload" to do a hard browser reload on history navigations.
htmx.config.modedefaults to 'same-origin', the fetch mode for AJAX requests. Can be 'cors', 'no-cors', or 'same-origin'
htmx.config.defaultSwapdefaults to innerHTML
htmx.config.indicatorClassdefaults to htmx-indicator
htmx.config.requestClassdefaults to htmx-request
htmx.config.includeIndicatorCSSdefaults to true (determines if the indicator styles are loaded)
htmx.config.defaultTimeoutdefaults to 60000 (60 seconds), the number of milliseconds a request can take before automatically being terminated
htmx.config.inlineScriptNoncedefaults to '', meaning that no nonce will be added to inline scripts
htmx.config.inlineStyleNoncedefaults to '', meaning that no nonce will be added to inline styles
htmx.config.extensionsdefaults to '', a comma-separated list of extension names to load (e.g., 'preload,optimistic')
htmx.config.sseconfiguration for Server-Sent Events (SSE) streams. An object with the following properties: reconnect (default: false), reconnectMaxAttempts (default: 10), reconnectDelay (default: 500ms), reconnectMaxDelay (default: 60000ms), reconnectJitter (default: 0.3), closeOnHide (default: false)
htmx.config.morphIgnoredefaults to ["data-htmx-powered"], array of attribute names to ignore when morphing elements (see Morphing)
htmx.config.morphScanLimitdefaults to 10, limits how many siblings to scan when matching elements during morphing. Increase for better accuracy with long lists without IDs (see Morphing)
htmx.config.morphSkipdefaults to undefined, CSS selector for elements that should be completely skipped during morphing (see Morphing)
htmx.config.morphSkipChildrendefaults to undefined, CSS selector for elements whose children should not be morphed (see Morphing)
htmx.config.noSwapdefaults to [204, 304], array of HTTP status codes that should not trigger a swap
htmx.config.implicitInheritancedefaults to false, if set to true attributes will be inherited from parent elements automatically without requiring the :inherited modifier
htmx.config.metaCharacterdefaults to undefined, allows you to use a custom character instead of : for attribute modifiers (e.g., - to use hx-get-inherited instead of hx-get:inherited)
htmx.config.versionthe version of the current htmx library

You can set them directly in javascript, or you can use a meta tag:

<meta name="htmx-config" content='{"defaultSwapStyle":"outerHTML"}'>