Mrs. Rini's laundry order board is open in the browser. The cashier clicks "Check status" on order #12, and the whole page reloads: the title, the menu and every order. All of that happens only to change one word, "Washing" to "Ironing". There are two usual ways to avoid it: write your own JavaScript, or add a framework such as React (a set of ready-made code for building interfaces).
htmx gives you another way. htmx is a small JavaScript library, which is ready-made code that you load into the page. With htmx, an HTML element can ask the server for something by itself. The server is the computer that sends pages to the browser. You only add attributes that start with hx- to the tag. This happens, in order:
- The element sends a request (a message from the browser to the server).
- The server replies with a piece of HTML.
- htmx puts that piece in the part of the page you point at.
The rest of the page does not change, and the page doesn't reload. Mrs. Rini works the same way on the whiteboard at her counter. When order #12 moves to "Ironing", Mrs. Rini doesn't rewrite the whole board. She only wipes one status box and writes the new status. A plain link works like rewriting the whole board. htmx works like Mrs. Rini: it changes only that one box, one element on the page.
Install htmx with one script tag
Add this one tag to the page:
<script src="https://unpkg.com/[email protected]"></script>
unpkg.comis a CDN: a server that shares library files from npm. npm is an online store where JavaScript libraries are kept as packages.htmx.orgis htmx's package name on npm.@2.0.11pins the version. It is the latest htmx 2 as of October 2026. The file is 52,182 bytes, about 16.5 KB once gzip-compressed. Without@version,https://unpkg.com/htmx.orggives whatever the latest version is at that time, so the file's content can change by itself one day.- You don't need
npm install, and there is no build step (a step that processes code before the browser can use it). Put this tag in the<head>, and htmx reads everyhx-attribute on the page.
htmx 2 or htmx 4? htmx 4.0.0 came out on 28 August 2026, but on npm it still has the label next. The label latest, which you get when you don't write a version, is still 2.0.11, and htmx.org says it moves to htmx 4 in 2027. This lesson uses htmx 2. An htmx 4 tutorial will look different in a few ways (tested in Chromium with 2.0.11 and 4.0.1):
| Topic | htmx 2 (this lesson) | htmx 4 |
|---|---|---|
hx-target on a wrapper element |
also applies to the elements inside it | only when written as hx-target:inherited |
| Error replies, 4xx and 5xx | not put into the page | put into the page |
| Event names | htmx:afterRequest |
htmx:after:request |
| How requests are sent | XMLHttpRequest | fetch() |
The first button: Check status
This is a piece of Mrs. Rini's order board. The server rendered this page, which means the server built its HTML. Order #12 still says "Washing".
<ul id="order-board">
<li id="order-12">
#12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
<button hx-get="/order/12/status" hx-target="#status-12">
Check status
</button>
</li>
</ul>
hx-get="/order/12/status": when the button is clicked, htmx sends a GET request to that address. GET is the kind of request that asks for data. A GET request should change nothing on the server.hx-target="#status-12": the server's reply goes into the element with the idstatus-12, not into the button itself. The value of this attribute is a CSS selector, so#means id.<span id="status-12">is that target element. It has to exist on the page.
hx-get and hx-target are plain HTML attributes. The browser only stores them, and htmx reads them. So htmx gives you AJAX (requests sent in the background, without reloading the page) through HTML attributes.
What happens when the button is clicked
| Step | What happens |
|---|---|
| 1. Click | htmx sees hx-get on the clicked button |
| 2. Request | the browser sends GET /order/12/status, plus the header HX-Request: true (a header is extra information that travels with the request) so the server knows it came from htmx |
| 3. Reply | the server replies with HTML: Ironing, only 7 bytes |
| 4. Swap | htmx makes that reply the content of #status-12. The rest of the page stays |
Before the click, the page reads #12 Mr. Darto, 3 kg: Washing. After the click, it reads #12 Mr. Darto, 3 kg: Ironing. Lines of JavaScript you wrote: 0. The JavaScript still exists, but it is inside htmx. The authors of htmx wrote that code once, and every button you make uses it.
The server replies with HTML, not JSON
The same update can be done three ways. One of them uses JSON, a text format for sending data. The sizes in this table were measured in this page's Lab, for the same status:
| Way | What the server sends | Size | Your own JavaScript |
|---|---|---|---|
Plain link <a href="/board"> |
the whole page | 1,136 bytes | 0 lines, but the whole page is drawn again |
| fetch + JSON | {"id":12,"status":"Ironing"} |
28 bytes | 5 lines |
| htmx | Ironing |
7 bytes | 0 lines |
The fetch + JSON way needs code like this on the page:
<button id="check-12">Check status</button>
<script>
document.querySelector('#check-12').addEventListener('click', async () => {
const res = await fetch('/api/order/12')
const order = await res.json()
document.querySelector('#status-12').textContent = order.status
})
</script>
That code waits for a click, gets the JSON from the server with fetch, then writes order.status into #status-12.
- With JSON, the server sends raw data, and code in the browser turns that data into something on screen. Every new screen needs code like this again.
- With htmx, the server builds the HTML (in Laravel, a PHP framework, from Blade files), and the browser only has to put it on the page. This approach is called hypermedia: the server sends HTML that is ready to show.
- The reply's size isn't the main point. For one word, HTML is smaller than JSON. For a long list, HTML is usually a little bigger because it carries tags. What you no longer need is the rendering code in the browser, the code that turns data into something on screen.
When htmx fits
- Fits: apps whose pages the server renders (Laravel, Django, Rails, PHP), such as admin panels, forms, filtered lists and dashboards.
- Fits less: apps whose state (the data the app is holding) is kept in the browser and changes all the time without the server. Examples are a drawing editor, a game, or an app that must work offline. For apps like that, an SPA (single-page application: an app where JavaScript builds the screens in the browser, for example with React or Vue) fits better.
Other options between htmx and an SPA. This lesson uses htmx because it works with any server and needs only HTML attributes.
| Option | When to pick it |
|---|---|
| Livewire | your project is Laravel, and you want to write interactive parts as a PHP class plus a Blade view |
| Alpine.js | you need small behaviour in the browser with no request, such as opening and closing a menu or tabs. Often used together with htmx |
| Hotwire Turbo | a similar way of working, the Rails default: links and forms work without a reload by themselves, plus Turbo Frames and Turbo Streams |
| Unpoly | similar to htmx, with overlays (modals, drawers, popups) and form validation against the server already built in |
⚠️ A button that does nothing, or a reply that shows raw
- The server replies with JSON. htmx puts any reply into the page as HTML. So
{"id":12,"status":"Ironing"}shows on the page just as it is. Reply with a piece of HTML. - The script tag is missing. Without htmx, nothing reads the
hx-attributes. The browser ignores attributes it doesn't know, with no error. The button does nothing when you click it. - The target doesn't exist. If it says
hx-target="#result"but the page has no element withid="result", htmx doesn't even send the request. In the exercise, both the button and#resultare checked. - The version isn't pinned. The address
https://unpkg.com/htmx.orgwith no version can switch to the next major version. htmx 4 is already on npm under the labelnext, and one day it becomes the latest version. Write[email protected].
TL;DR: htmx is a small JavaScript library, which is ready-made code. It gives HTML elements AJAX: sending a request to the server, the computer that sends pages, without reloading the page. You use it through attributes that start with hx-. htmx is loaded with one tag, <script src="https://unpkg.com/[email protected]">. On a button, hx-get="/order/12/status" sends a GET request (a request for data) to that address when the button is clicked, and hx-target="#status-12" points at the element that gets the reply. The server replies with a piece of HTML (Ironing, 7 bytes), not JSON (raw data). htmx puts that piece on the page, with 0 lines of your own JavaScript. A plain link sends the whole page again (1,136 bytes). fetch + JSON needs your own code to turn the data into something on screen. htmx fits apps whose HTML the server builds, and fits less when the data is kept and changed in the browser.
⚡ Install htmx with one tag
Write:
<script src="https://unpkg.com/[email protected]"></script>
Meaning: htmx is a small JavaScript library: ready-made code that lets HTML elements send requests to the server, the computer that sends pages. Put this tag in the <head>, with no npm install and no build step. @2.0.11 pins the version, so the file that is loaded can't change by itself.
⚡ A button that asks the server
Write:
<li id="order-12">
#12 Mr. Darto, 3 kg: <span id="status-12">Washing</span>
<button hx-get="/order/12/status" hx-target="#status-12">
Check status
</button>
</li>
Guess first: hx-get holds the address that is requested from the server when the button is clicked, and hx-target points at the element that gets the reply. The server replies Ironing. The button is clicked once. Which part of the page changes?
Only the content of #status-12, from Washing to Ironing. The page doesn't reload, and you wrote 0 lines of JavaScript.
Meaning: hx-get and hx-target are HTML attributes that htmx reads. The value of hx-target is a CSS selector, so #status-12 points at the element with the id status-12. That element has to be on the page.
⚡ The server replies with HTML, not JSON
Guess first: the server replies to that button with JSON (a text format for sending data), {"id":12,"status":"Ironing"}. What shows on the page?
The JSON shows on the page just as it is. htmx puts the reply into the page as HTML, unchanged.
Meaning: For htmx, the server has to send a piece of HTML that is ready to show (Ironing, 7 bytes), not raw data such as JSON. That is why you don't need to write code in the browser that turns data into something on screen.