What if you could build interactive web apps without writing JavaScript? htmx makes HTML do what JavaScript frameworks do — with zero build step.
What is htmx?
htmx gives you access to AJAX, CSS Transitions, WebSockets, and Server-Sent Events directly in HTML, using attributes. No JavaScript required.
Quick Start
<script src="https://unpkg.com/htmx.org@2.0.0"></script>
That is it. No npm, no bundler, no build step.
Click to Load
<button hx-get="/api/users" hx-target="#user-list" hx-swap="innerHTML">
Load Users
</button>
<div id="user-list"></div>
When clicked, htmx makes a GET to /api/users and puts the HTML response into #user-list.
Search as You Type
<input type="search" name="q"
hx-get="/search"
hx-trigger="input changed delay:300ms"
hx-target="#results">
<div id="results"></div>
Infinite Scroll
<tr hx-get="/contacts?page=2"
hx-trigger="revealed"
hx-swap="afterend">
<td>Loading more...</td>
</tr>
Form Submit
<form hx-post="/contacts"
hx-target="#contact-list"
hx-swap="beforeend">
<input name="name" placeholder="Name">
<input name="email" placeholder="Email">
<button type="submit">Add Contact</button>
</form>
Delete with Confirmation
<button hx-delete="/contacts/1"
hx-confirm="Are you sure?"
hx-target="closest tr"
hx-swap="outerHTML">
Delete
</button>
Key Attributes
| Attribute | What it does |
|---|---|
| hx-get | GET request |
| hx-post | POST request |
| hx-put | PUT request |
| hx-delete | DELETE request |
| hx-target | Where to put response |
| hx-swap | How to swap content |
| hx-trigger | What triggers the request |
| hx-confirm | Confirmation dialog |
| hx-indicator | Loading indicator |
Swap Strategies
-
innerHTML— Replace inner HTML (default) -
outerHTML— Replace entire element -
beforeend— Append inside element -
afterend— Insert after element -
delete— Delete the target -
none— Just make the request
Loading Indicators
<button hx-get="/slow-endpoint" hx-indicator="#spinner">
Fetch Data
<span id="spinner" class="htmx-indicator">Loading...</span>
</button>
Server-Sent Events
<div hx-ext="sse" sse-connect="/events">
<div sse-swap="message">Waiting for messages...</div>
</div>
htmx vs React
| Feature | htmx | React |
|---|---|---|
| Bundle Size | 14KB | 140KB+ |
| Build Step | None | Required |
| Language | HTML | JSX |
| State | Server | Client |
| SEO | Built-in | SSR needed |
| Learning Curve | Minutes | Weeks |
Need structured data for your web app? Check out my Apify actors — get clean data from any website. For custom solutions, email spinov001@gmail.com.
Top comments (0)