--- layout: layout.njk title: htmx - high power tools for html ---
## [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 lets 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
Click Me!
``` This tells htmx: > "When a user clicks on this div, 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-` prefix when using htmx: ``` html Click Me! ``` ## [Installing](#installing) Htmx is a dependency-free javascript library. It can be used via [NPM](https://www.npmjs.com/) as "`htmx.org`" or downloaded or included from [unpkg](https://unpkg.com/browse/htmx.org/) or your other favorite NPM-based CDN: ``` html ``` ## [AJAX](#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) | Issues a `GET` request to the given URL| | [hx-post](/attributes/hx-post) | Issues a `POST` request to the given URL | [hx-put](/attributes/hx-put) | Issues a `PUT` request to the given URL | [hx-patch](/attributes/hx-patch) | Issues a `PATCH` request to the given URL | [hx-delete](/attributes/hx-delete) | 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
Put To Messages
``` This tells the browser: > When a user clicks on this div, issue a PUT request to the URL /messages and load the response into the div ### [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) 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!]
``` If you want a request to only happen once, you can use the `once` modifier for the trigger: ```html
[Here Mouse, Mouse!]
``` There are few other modifiers you can use for trigger: * `changed` - only issue a request if the value of the element has changed * `delay: