--- website: https://htmx.org/ obj: concept --- # htmx htmx is a library that allows you to access modern browser features directly from [HTML](HTML.md), rather than using javascript. htmx extends and generalizes the core idea of [HTML](HTML.md) as a hypertext, opening up many more possibilities directly within the language: - Now any element, not just anchors and forms, can issue an [HTTP](HTTP.md) request - Now any event, not just clicks or form submissions, can trigger requests - Now any [HTTP](HTTP.md) verb, 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](HTML.md)_, not _[JSON](../files/JSON.md)_. 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. # Installing Htmx is a dependency-free, browser-oriented javascript library. This means that using it is as simple as adding a ` ``` ## 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/dist/htmx.min.js) and add it to the appropriate directory in your project and include it where necessary with a ` ``` You can also add extensions this way, by downloading them from the `ext/` directory. # AJAX The core of htmx is a set of attributes that allow you to issue AJAX requests directly from [HTML](HTML.md): |Attribute|Description| |---|---| |[hx-get](https://htmx.org/attributes/hx-get/)|Issues a `GET` request to the given URL| |[hx-post](https://htmx.org/attributes/hx-post/)|Issues a `POST` request to the given URL| |[hx-put](https://htmx.org/attributes/hx-put/)|Issues a `PUT` request to the given URL| |[hx-patch](https://htmx.org/attributes/hx-patch/)|Issues a `PATCH` request to the given URL| |[hx-delete](https://htmx.org/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](https://htmx.org/docs/#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 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](https://htmx.org/attributes/hx-trigger/) attribute to specify which event will cause the request. #### 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: