+++ title = "Documentation" [extra] custom_classes = "wide-content" +++
## htmx in a Nutshell {#introduction} htmx is a library that allows you to access modern browser features directly from HTML, rather than using javascript. To understand htmx, first let's take a look at an anchor tag: ```html Blog ``` This anchor tag tells a browser: > "When a user clicks on this link, issue an HTTP GET request to '/blog' and load the response content > into the browser window". With that in mind, consider the following bit of HTML: ```html ``` This tells htmx: > "When a user clicks on this button, issue an HTTP POST request to '/clicked' and use the content from the response > to replace the element with the id `parent-div` in the DOM" htmx extends and generalizes the core idea of HTML as a hypertext, opening up many more possibilities directly within the language: * Now any element, not just anchors and forms, can issue an HTTP request * Now any event, not just clicks or form submissions, can trigger requests * Now any [HTTP verb](https://en.wikipedia.org/wiki/HTTP_Verbs), not just `GET` and `POST`, can be used * Now any element, not just the entire window, can be the target for update by the request Note that when you are using htmx, on the server side you typically respond with *HTML*, not *JSON*. This keeps you firmly within 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) without even needing to really understand that concept. It's worth mentioning that, if you prefer, you can use the [`data-`](https://html.spec.whatwg.org/multipage/dom.html#attr-data-*) prefix when using htmx: ```html Click Me! ``` Finally, [Version 1](https://v1.htmx.org) of htmx is still supported and supports IE11. ## 1.x to 2.x Migration Guide If you are migrating to htmx 2.x from [htmx 1.x](https://v1.htmx.org), please see the [htmx 1.x migration guide](@/migration-guide-htmx-1.md). If you are migrating to htmx from intercooler.js, please see the [intercooler migration guide](@/migration-guide-intercooler.md). ## Installing Htmx is a dependency-free, browser-oriented javascript library. This means that using it is as simple as adding a ` ``` An unminified version is also available as well: ```html ``` While the CDN approach is extremely simple, you may want to consider [not using CDNs in production](https://blog.wesleyac.com/posts/why-not-javascript-cdn). ### Download a copy The next easiest way to install htmx is to simply copy it into your project. Download `htmx.min.js` [from unpkg.com](https://unpkg.com/htmx.org@2.0.4/dist/htmx.min.js) and add it to the appropriate directory in your project and include it where necessary with a ` ``` ### npm For npm-style build systems, you can install htmx via [npm](https://www.npmjs.com/): ```sh npm install htmx.org@2.0.4 ``` After installing, you’ll need to use appropriate tooling to use `node_modules/htmx.org/dist/htmx.js` (or `.min.js`). For example, you might bundle htmx with some extensions and project-specific code. ### Webpack If you are using webpack to manage your javascript: * Install `htmx` via your favourite package manager (like npm or yarn) * Add the import to your `index.js` ```js import 'htmx.org'; ``` If you want to use the global `htmx` variable (recommended), you need to inject it to the window scope: * Create a custom JS file * Import this file to your `index.js` (below the import from step 2) ```js import 'path/to/my_custom.js'; ``` * Then add this code to the file: ```js window.htmx = require('htmx.org'); ``` * Finally, rebuild your bundle ## AJAX The core of htmx is a set of attributes that allow you to issue AJAX requests directly from HTML: | Attribute | Description | |----------------------------------------|--------------------------------------------| | [hx-get](@/attributes/hx-get.md) | Issues a `GET` request to the given URL | | [hx-post](@/attributes/hx-post.md) | Issues a `POST` request to the given URL | | [hx-put](@/attributes/hx-put.md) | Issues a `PUT` request to the given URL | | [hx-patch](@/attributes/hx-patch.md) | Issues a `PATCH` request to the given URL | | [hx-delete](@/attributes/hx-delete.md) | Issues a `DELETE` request to the given URL | Each of these attributes takes a URL to issue an AJAX request to. The element will issue a request of the specified type to the given URL when the element is [triggered](#triggers): ```html ``` This tells the browser: > When a user clicks on this button, issue a PUT request to the URL /messages and load the response into the button ### Triggering Requests {#triggers} By default, AJAX 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](@/attributes/hx-trigger.md) attribute to specify which event will cause the request. Here is a `div` that posts to `/mouse_entered` when a mouse enters it: ```html
[Here Mouse, Mouse!]
``` #### Trigger Modifiers A trigger can also have a few 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
[Here Mouse, Mouse!]
``` Other modifiers you can use for triggers are: * `changed` - only issue a request if the value of the element has changed * `delay: