# Going deeper with htmx 4 -- combining htmx and JavaScript

DevFeed: [Going deeper with htmx 4 -- combining htmx and JavaScript](<https://devfeed.tech/articles/going-deeper-with-htmx-4-combining-htmx-and-javascript-117128.md>)

Original publisher: [Read original article](<https://www.infoworld.com/article/4237122/going-deeper-with-htmx-4-combining-htmx-and-javascript.html>)

Author: Serdar Yegulalp

Published: 2026-11-04T09:00:00Z

Content type: article

Language: en

Sources: [InfoWorld](<https://devfeed.tech/sources/infoworld.md>)

Topics: [htmx](<https://devfeed.tech/topics/htmx.md>), [JavaScript](<https://devfeed.tech/topics/javascript.md>), [Front end](<https://devfeed.tech/topics/frontend.md>), [Stimulus](<https://devfeed.tech/topics/stimulus.md>), [Python](<https://devfeed.tech/topics/python.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [development-approaches](<https://devfeed.tech/tags/development-approaches.md>), [development-approaches-javascript-programming-languages-software-development-web-development](<https://devfeed.tech/tags/development-approaches-javascript-programming-languages-software-development-web-development.md>), [events](<https://devfeed.tech/tags/events.md>), [htmx](<https://devfeed.tech/tags/htmx.md>), [javascript](<https://devfeed.tech/tags/javascript.md>), [language-en](<https://devfeed.tech/tags/language-en.md>), [programming-languages](<https://devfeed.tech/tags/programming-languages.md>), [software-development](<https://devfeed.tech/tags/software-development.md>), [storytype-feature](<https://devfeed.tech/tags/storytype-feature.md>), [web-development](<https://devfeed.tech/tags/web-development.md>)

## AI overview

htmx supports both incremental adoption alongside existing JavaScript and migration toward htmx. Inline `hx-on` attributes keep custom behavior close to elements and consistent with other `hx-` attributes; the `htmx` API offers more flexible hooks, including `on()`, `onLoad()` for dynamically added content, and `ajax()` for requests processed and inserted by htmx. In htmx 4, `q()` provides selector-based DOM operations such as toggling classes, triggering events, and inserting HTML, usable from attributes or custom JavaScript. The excerpt cautions that combining reactive front-end frameworks can cause side effects, while noting the `hx-alpine-compat` extension for keeping Alpine.js behaviors active in htmx-managed components. It also names third-party htmx support for Django and FastAPI, Jinja2 Fragments for partial rendering, and native support in Litestar.

## Source excerpt

htmx lets you add rich functionality to web pages without writing JavaScript. But it also lets you integrate JavaScript if you need it, either by adding functions in-line to elements or by writing more complex hooks for events. These behaviors let you take web pages that already use JavaScript, and either migrate to htmx entirely or add htmx only where it's needed -- for instance, in new parts of an application. Generally, htmx works best with green-field applications, since you can start with a non-interactive page and just layer in the behaviors you want as they're needed. But htmx can also work as an add-on for a page that already uses JavaScript, or as a migration target away from existing JavaScript. Using htmx with JavaScript When you want to integrate htmx with hand-rolled JavaScript, htmx offers you several ways to do it. Some are extremely basic, but tightly coupled to the objects they work with. Others are more advanced and flexible, but require more work to use. Using hx-on attributes A basic way to add JavaScript behaviors to objects is through a very htmx-like mechanism, the hx-on attributes. Add one of these to a given object and you can provide code or function calls inline. For example: <button hx-on:click="console.log(`${this} clicked`);"> Click here to log to the console The inline code can be a whole code block, as shown here, or a function call to your own code. One advantage of using hx-on is consistency with other hx- attributes. If all your existing interaction is powered by htmx, adding more hx- attributes for custom functionality keeps things uniform. On the other hand, if you want your custom functionality to be mechanically distinct from htmx, consider the approach discussed next: registering callbacks. Using the htmx API The more complex way to "wire up" JavaScript functionality for htmx-enabled pages is to use the htmx object and its methods. With htmx you can register callback functions for any behavior. The htmx object provides a set of