# Documentation ## htmx in a Nutshell htmx extends HTML's built-in concept of [hypermedia controls](https://dl.acm.org/doi/fullHtml/10.1145/3648188.3675127). To understand htmx, let's take a look at the two most widely known such controls: `` (anchors, aka "links") & `
`s. Here is a basic, boring anchor tag: ```html Blog ``` If you get past how pedestrian this link is, it's actually a pretty amazing little bit of technology: when a user clicks on this link in a browser, the browser will issue an HTTP `GET` request to `/blog`. The browser then loads the HTML response returned by the server into the browser's window. Forms are a bit more complicated: ```html
``` When a user submits this form (by, say, clicking on the "Submit" button) a browser will issue an HTTP `POST` request to `/register`. Again, the browser will load the HTML response to this request into the browser window. The common pattern here is that the user performs an action, say, a click, and the browser issues an HTTP request to a server and then loads the response HTML into the browser's window. ### How htmx Extends This Idea The core idea of htmx is to use a few custom attributes to _generalize_ this idea: * Any element can issue an HTTP request * Any event can trigger that request * The response HTML can be placed anywhere in the DOM This small extension turns out to dramatically boost the expressiveness of HTML. Here is a sample htmx-powered button: ```html ``` The htmx attributes start with `hx-`. Let's go through each one: * `hx-post="/clicked"` - this button should issue a POST request to the `/clicked` relative URL * `hx-trigger="click"` - it should issue the request when the button is clicked * `hx-target="#output"` - it should target the element with the id `output` with the HTML in the response to this request * `hx-swap="outerHTML"` - it should replace the element entirely with that html These four attributes let you specify how and when this button should issue a request and where in the DOM the resulting HTML should be placed. One thing to note: the `hx-trigger` here is redundant. If it was omitted htmx would use the default trigger event which, in the case of buttons, is a click. One important thing to understand is that htmx expects _HTML_ from the server. In this case the server would return a _partial_ bit of HTML, say a `
`, to replace the button. What htmx does _not_ expect is JSON. Because htmx works in terms of HTML it follows the [original web programming model](https://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm), using [Hypertext As The Engine Of Application State](https://en.wikipedia.org/wiki/HATEOAS) (HATEOAS). This makes developing with htmx feel much more like traditional web development than most front-end libraries today. ## Installing htmx htmx is a single JavaScript file with no dependencies. No build step is required to use it. ### Installing CDN To install htmx as a vanilla JavaScript library via the jsdeliver CDN, add this in your `` tag: ```html ``` Or, if you wish the unminified version (perhaps for debug reasons) use: ```html ``` #### ES Module To install htmx as a JavaScript ES Module via the jsdeliver CDN, add this in your `` tag: ```html ``` or, unminified: ```html ``` ### Downloading/Vendoring htmx While a CDN is convenient, you should consider [self-hosting in production](https://blog.wesleyac.com/posts/why-not-javascript-cdn). 1. Download htmx.min.js 2. Save it to your project (e.g., `/js/htmx.min.js`) 3. Add this in your `` tag: ```html ``` This is sometimes called "vendoring" htmx. You can also download: * htmx.js (unminified) * htmx.esm.min.js (ES module) * htmx.esm.js (ES module, unminified) ### Installing htmx via NPM You can install htmx via the standard ```sh npm install htmx.org@4.0.0 ``` ```javascript import 'htmx.org'; ``` ```javascript import htmx from 'htmx.org'; // Now you can use htmx.ajax(), htmx.find(), etc. ``` ### Installing htmx + the core extensions: htmax.js htmx has many useful [extensions](https://four.htmx.org/extensions) that add functionality to it. If you want to install a distribution that ships with most of the useful extensions already installed so you don't have to think about it, you can use the [htmax.js](https://four.htmx.org/docs/htmax) distribution. ## Migrating From htmx 2.x to 4.x There are three major behavioral changes between htmx 2.x and 4.x: * In htmx 2.0 attribute inheritance is _implicit_ by default while in 4.0 it is _explicit_ by default * To restore the 2.0 behavior, you can set the [`htmx.config.implicitInheritance`](https://four.htmx.org/reference/config/htmx-config-implicitInheritance) setting to `true` * In htmx 2.0, `400` and `500` response codes are _not_ swapped by default, whereas in htmx 4.0 these requests _will_ be swapped * To restore the 2.0 behavior, you can set the [`htmx.config.noSwap`](https://four.htmx.org/reference/config/htmx-config-noSwap) setting to `[204, 304, '4xx', '5xx']` * In htmx 2.0, history used a local cache snapshot for history navigation, while in 4.0 it issues a request to the server to get the full page to restore * htmx includes an [`hx-history-cache`](https://four.htmx.org/extensions/hx-history-cache) extension if you wish to have a local cache. This extension integrates with Alpine.js and hx-live seamlessly. Event names were also standardized/rationalized. ### Upgrade Checker To make upgrading easier, htmx 4 ships with a command-line upgrade tool that scans your templates and JS files for htmx 2 code that needs updating. It checks for the following: * Removed attributes * Old event names * Attribute inheritance patterns * Extension changes, etc. You can run the upgrade checker via `npx`: ```bash npx htmx.org@4.0.0 upgrade-check -- ./path/to/project/root npx htmx.org@4.0.0 upgrade-check --ext .vue ./path/to/project/root ``` By default, the tool scans `.html`, `.php`, `.js`, `.ts`, `.jinja`, `.jinja2`, `.j2`, `.erb`, and `.hbs` files. You can add additional HTML-like file extensions via the `--ext` argument. Output is `file:line` format, clickable in most editors. For more details see [What's New in 4.0](https://four.htmx.org/docs/whats-new-in-htmx-4) Extension authors who need to port their extensions to htmx 4 can refer to [Migrating Extensions to 4.0](https://four.htmx.org/docs/extension-htmx-4-migration-guide) ## Issuing Requests & Handling Responses The crux of htmx is issuing HTTP request in response to events and then placing the response HTML into the document. The core attributes for driving this behavior are: * [`hx-get`](https://four.htmx.org/reference/attributes/hx-get) - issues an HTTP `GET` * [`hx-post`](https://four.htmx.org/reference/attributes/hx-post) - issues an HTTP `POST` * [`hx-put`](https://four.htmx.org/reference/attributes/hx-put) - issues an HTTP `PUT` * [`hx-patch`](https://four.htmx.org/reference/attributes/hx-patch) - issues an HTTP `PATCH` * [`hx-delete`](https://four.htmx.org/reference/attributes/hx-delete) - issues an HTTP `DELETE` * [`hx-query`](https://four.htmx.org/reference/attributes/hx-query) - issues an HTTP `QUERY` These attributes can be placed on any element to tell that element to issue a request when triggered: ```html ``` As of htmx 4.0, you can also use the following alternative attributes: * [`hx-action`](https://four.htmx.org/reference/attributes/hx-action) - The URL to issue a request to * [`hx-method`](https://four.htmx.org/reference/attributes/hx-method) - The HTTP Action to use ```html ``` This is inspired by the syntax that forms use: ```html
``` ### Triggering Requests By default, HTTP requests are triggered by the "natural" event of an element: * `input`, `textarea` & `select` are triggered on the `change` event * `form` is triggered on the `submit` event * everything else is triggered by the `click` event If you want different behavior you can use the [`hx-trigger`](https://four.htmx.org/reference/attributes/hx-trigger) attribute to specify which event will cause the request. Here is a `div` that issues an HTTP `POST` to `/mouse_entered` when a mouse enters it: ```html
Mouse Trap
``` #### Trigger Modifiers A trigger can also have additional modifiers that change its behavior. For example, if you want a request to only happen once, you can use the `once` modifier for the trigger: ```html
Mouse Trap
``` Other modifiers you can use for triggers are: | Modifier | Description | |----------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `changed` | only issue a request if the value of the element has changed | | `delay: