October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Get started with htmx 4: dynamic web pages without custom JavaScript

htmx 4 lets HTML elements request server-rendered fragments and swap them into the page. Here is how to install version 4.0.0, build a first interaction, and handle the upgrade changes.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

htmx 4 lets ordinary HTML elements send HTTP requests and lets the browser replace part of the page with HTML the server sends back. For many common interactions, you write attributes in your markup instead of a custom client-side script for each one. The library itself is JavaScript, so the accurate version of the promise is “little or no hand-written JavaScript for common interactions,” not “no JavaScript at all.”

What htmx 4 is, and what “without JavaScript” actually means

htmx is a single JavaScript file with no dependencies. You add it to a page, and then attributes such as hx-get and hx-post tell elements to make requests. Other attributes control what starts the request (hx-trigger), where the response goes (hx-target), and how it is inserted (hx-swap). The server is still responsible for producing the HTML. htmx does not generate it for you.

As an Amazon Associate I earn from qualifying purchases.

That split is the point of the library. Browsers already know how to follow links and submit forms. htmx extends those hypermedia controls so that a button, a input, or a list item can also request a fragment of HTML and place it somewhere on the page. The result fits server-rendered applications well. It does not remove the need to understand HTTP, browser behavior, or how the server builds its responses, and it does not make client-side scripting unnecessary for every feature.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Version status and installation

The htmx project announced version 4.0.0 on 28 August 2026. Its announcement said that version 2.x would stay on the npm latest tag, and 4.0 on the next tag, until some point in early 2027. The purpose was to avoid silently upgrading people who load htmx from unversioned CDN URLs. Check the tag yourself before relying on it. If you need 4.x, name the version explicitly.

The project documents three ways to load the library. Pick one based on how much control you want over updates.

Method How to do it Trade-off
Versioned CDN Load the 4.0.0 file from a CDN URL that includes the version number, copied from the official documentation No local files to manage. You depend on the CDN being reachable, and you must update the version string yourself when you upgrade.
Vendored file Download the 4.0.0 file and serve it from your own domain, under whatever filename you save it as Full control over caching and availability. You own the upgrade process. The project’s documentation suggests considering self-hosting for production.
npm npm install [email protected], then serve the installed file through your build or static asset pipeline Version is recorded in your lockfile. Pinning the exact version avoids depending on the latest tag, which still points to 2.x.

None of these methods requires a build step for basic use. A plain script tag is enough.

Your first interaction

The smallest useful example is a button that fetches a fragment from the server and places it inside a container. The steps below assume a server route at /greeting that returns a short HTML snippet, not a full page.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Load htmx 4.0.0. Add a script tag that points to your chosen copy of the 4.0.0 file, placed in the document head or before the closing body tag.
  2. Add a target container. Give an empty element an id, for example <div id="result"></div>. This is where the response will appear.
  3. Mark the trigger element. Add hx-get="/greeting", hx-target="#result", and hx-swap="innerHTML" to a button: <button hx-get="/greeting" hx-target="#result" hx-swap="innerHTML">Say hello</button>.
  4. Return an HTML fragment from the server. The route should send only the markup for the piece you want to replace, such as <p>Hello from the server.</p>, not a complete document with a head and body.
  5. Test it. Click the button. The paragraph should appear inside the container, and the page should not reload. If nothing changes, open your browser’s developer tools network panel and confirm that the request reached the route and returned a 200 response with the expected markup.

Once this works, the same pattern extends to forms (hx-post), partial page updates after an action, and lists that refresh in place.

The mental model to hold onto

  • Markup describes the interaction: which element starts it, which URL it calls, and where the answer goes.
  • The browser sends a normal HTTP request.
  • The server responds with HTML, not JSON that your own code has to render.
  • htmx inserts that HTML into the document at the target you named.

This model works best when the server already renders your pages. If your application is built around a client-side framework that owns all rendering, htmx is a poor fit for those parts, and you should keep your existing approach there.

Upgrading from htmx 2: the changes that break things

Most of the 4.0 changes are about what you can rely on in the markup and the event system. The release announcement describes three major user-facing changes: attribute inheritance is explicit, event names are standardized, and history no longer uses localStorage by default. It also says the internal move from XMLHttpRequest to the native fetch API should be transparent for most users. The upgrade guide covers the full list of breaking changes and new attributes.

Attribute inheritance now needs :inherited

In earlier versions, attributes placed on a parent element could affect its descendants. In 4.0, an attribute that should pass down the DOM tree needs the :inherited suffix. Search your templates for attributes set on containers and check whether any child elements depended on them. Those children will stop behaving as expected until you add the suffix.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Event names are standardized

Event names now follow a consistent pattern. For example, htmx:afterRequest becomes htmx:after:request. Any listener written against the old name will not fire. Update each listener name, then trigger one request in each affected area to confirm the listener runs.

Removed helper functions

Some helpers were removed. The upgrade guide gives htmx.addClass() as an example and recommends the native DOM method instead. Replace htmx.addClass(element, "name") with element.classList.add("name"). Check the upgrade guide for every removed API your code calls.

History caching works differently

htmx 4 does not cache page content in localStorage by default. When a user navigates back, the page content is fetched again. If your application depended on the old cache, expect more server requests on back navigation. If you need local caching, the optional hx-history-cache extension stores pages in sessionStorage instead.

Run the upgrade check first

The release announcement provides a command you can run against your project before making manual changes: npx [email protected] upgrade-check. Treat its output as a starting list, not a complete audit. The upgrade guide is still the authority on what changed.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Optional capabilities

The 4.x documentation describes extensions for server-sent events, WebSockets, file downloads, and compatibility with Alpine.js. The docs also describe hx-live, a DOM-oriented reactive scripting option introduced in htmx 4. None of these is needed for a first page. Add them when a specific feature calls for one, and read the extension’s own documentation before you depend on it.

Security: htmx expands what HTML can do

The project’s documentation warns that because htmx makes HTML more expressive, an attacker who can inject HTML into your application may be able to abuse that expressiveness. Attributes such as hx-get are not a safety feature. Escape user-supplied content on the server, validate input there, and treat every fragment you return as output that must be safe to render. htmx does not replace careful server-side handling.

Code size and what the project claims

The htmx homepage describes the library as small (about 16k minified and gzipped), dependency-free, and extendable. It also states that the library reduces code base sizes by 67% compared with React. That comparison is the project’s own claim. The homepage does not describe how the comparison was measured, so treat it as the project’s assertion rather than an independent benchmark. Size is only one factor. The better test is whether your own interactions need less custom script once you use htmx.

Further reading

The htmx homepage promotes Hypermedia Systems, a book about building hypermedia-driven applications with htmx and related tools. It is a reasonable next step for broader background on the architecture. It is not an installation manual for htmx 4, and we did not verify its current availability or pricing.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The Bottom Line

htmx 4 is a practical choice for server-rendered pages that need partial updates without a client-side framework. Pin 4.0.0 explicitly, work through the three upgrade checks if you are migrating, and keep server-side output escaping in place.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.