Lesson 27 of 28 ยท Practice and Review
Interview Questions
30 HTMX and Alpine.js interview questions with concise answers: hypermedia, swapping, triggers, reactivity, stores, security, testing and when to pick them.
Interviewers who see HTMX or Alpine.js on a resume rarely ask you to recite attribute lists. They want to know whether you understand why these tools exist, what trade-offs they bring, and whether you can keep a page safe and maintainable. This page collects 30 common questions in four groups: hypermedia and HTMX, Alpine.js, using both, and judgment questions (security, testing, choosing).
Each answer is hidden. Say yours out loud first, then open the answer and compare. Two small code puzzles at the end let you predict behavior before you run it.
A good answer has
Four groups below
Hypermedia and HTMX
1. What is hypermedia, and what does "HTML over the wire" mean?
Hypermedia means the server sends the data together with the controls for the next actions, as HTML does with links and forms. "HTML over the wire" means that after the first page load, interactions fetch ready-made HTML fragments from the server and insert them into the page, instead of fetching JSON and building the HTML in the browser. See the introduction.
2. What problem does HTMX solve?
Plain HTML only lets links and forms trigger requests, only with GET and POST, and every response replaces the whole page. HTMX lets any element trigger any HTTP method on any event, and put the response into any place on the page. It does this through attributes, with no JavaScript written by you.
3. Explain hx-get, hx-trigger, hx-target and hx-swap.
hx-get says which URL to fetch. hx-trigger says which event starts the request (the natural event of the element if omitted: click for buttons, submit for forms, change for inputs). hx-target says which element receives the response (the element itself if omitted). hx-swap says how the response is inserted (innerHTML by default). See Your First HTMX Request.
4. Name five swap values.
innerHTML (default), outerHTML, beforebegin, afterbegin, beforeend, afterend, delete and none. The beforeend value appends inside the target, which is the usual choice for "add an item to a list". See Targets and Swap Strategies.
5. What is an out-of-band swap?
It is a way for one response to update several places. Elements in the response marked hx-swap-oob="true" (or with a swap style) are swapped into the element with the same id elsewhere in the page, in addition to the normal swap. A common use is updating a cart badge or counter while the main target shows the list.
6. How can you delay a request until the user stops typing?
hx-trigger="input changed delay:300ms". delay waits for quiet, restarting on each event, and changed skips the request if the value did not change. throttle:1s is the alternative that allows at most one request per interval. See Verbs, Triggers and Parameters.
7. What gets sent with a request?
For a request from a form element or from inside a form, the form's fields. For a non-GET request from an input, the input's own name=value. You can add data with hx-vals (JSON), hx-include (other elements' values) and filter with hx-params. HTMX also adds headers such as HX-Request: true, HX-Target, HX-Trigger and HX-Current-URL.
8. Which header lets the server know a request came from HTMX, and why is that useful?
HX-Request: true. The server can return a fragment for HTMX requests and a full page for ordinary navigation, so the same URL works with and without JavaScript. Remember to send Vary: HX-Request if caching is involved.
9. Does HTMX swap a 404 or 500 response?
No, by default only 2xx responses are swapped (and 204 No Content swaps nothing). Other statuses fire htmx:responseError. You can change the behavior in htmx:beforeSwap (detail.shouldSwap = true) or through htmx.config.responseHandling. See Events, Headers and JavaScript Integration.
10. How do you show a loading indicator?
During a request HTMX adds the class htmx-request to the element (or to the element named by hx-indicator). CSS shows a spinner when that class is present. You can also disable the button with hx-disabled-elt. See Loading States.
11. What does hx-boost do?
It turns ordinary links and forms inside an element into AJAX requests that swap the <body>, with history support, so the site feels like a single-page app. If JavaScript fails, the links still work as normal links, which is progressive enhancement.
12. How do you keep the URL and the back button working?
hx-push-url="true" pushes the requested URL into the history; hx-replace-url replaces the current entry. Servers can set HX-Push-Url in a response. HTMX caches snapshots of pages so Back restores quickly. Design URLs so that opening them directly returns a full page. See URLs, History and Navigation.
13. Name three server response headers HTMX understands.
HX-Redirect (client-side redirect), HX-Trigger (fire client events, optionally with JSON detail), HX-Retarget and HX-Reswap (change target or swap style), HX-Push-Url and HX-Refresh.
14. How do you run your own JavaScript around HTMX requests?
Listen to its events (htmx:configRequest to change headers or parameters, htmx:beforeSwap, htmx:afterSwap, htmx:afterSettle), or use htmx.ajax() and htmx.process() from code. Events bubble, so you can listen once on document.body.
15. How do you handle two overlapping requests from one element?
Use hx-sync, for example hx-sync="this:replace" to abort the old request when a new one starts, which prevents a slow earlier response from overwriting a newer one.
Alpine.js
16. What is Alpine.js, in one sentence?
A small library that adds reactive behavior to HTML with attributes (x-data, x-show, @click) without a build step, sometimes described as "Tailwind for JavaScript". See Reactive State with x-data.
17. What does x-data do, and what is its scope?
It declares a component and its reactive state as a JavaScript object. All directives inside that element (including nested elements) can read and change the data. Nested x-data creates a child scope that can read the parent's data.
18. x-show versus x-if?
x-show toggles display: none and keeps the element in the DOM, so it is cheap to toggle and keeps state. x-if adds and removes the element completely and must be on a <template>. Use x-if when the content is heavy or should not exist at all. See Conditionals and Loops.
19. How does reactivity work in Alpine?
Alpine wraps the data in a JavaScript Proxy. When an expression reads a property, Alpine records the dependency; when you assign to that property, only the expressions that depend on it run again and update the DOM.
20. What is the difference between x-bind and x-model?
x-bind (the : shorthand) is one-way: it sets an attribute from data. x-model is two-way: it sets an input's value from data and writes user input back into the data. See Forms and x-model.
21. What is x-cloak for?
It hides an element until Alpine has initialized it. You need a CSS rule such as [x-cloak] { display: none !important }. Without it, you can see unstyled or hidden content flash during page load.
22. When would you use $dispatch?
To send a custom DOM event upward so another component can react, for example a child notifies the page that an item was saved. Listen with @saved.window="..." for events that must cross sibling components. See Events, Modifiers and Refs.
23. Alpine.data versus Alpine.store?
Alpine.data registers a reusable component definition: every use creates its own state. Alpine.store registers one global shared object that every component can read as $store.name. Use data for repeated widgets and a store for things like a cart count or a theme. See Reusable Components, Alpine.data and Stores.
24. Name some official Alpine plugins.
Persist (localStorage), Focus (focus trapping), Collapse (animated height), Mask (input masks), Intersect (visibility events), Anchor (positioning), Morph (HTML diffing) and Sort (drag to reorder). See Plugins.
Using both
25. How do you split work between HTMX and Alpine?
HTMX owns server state: lists, records, anything the server decides. Alpine owns UI state: open or closed, active tab, a draft in an input. If you find yourself sending a request for something that the browser could decide alone, use Alpine; if you find yourself duplicating server rules in the browser, use HTMX. See Using HTMX and Alpine.js Together.
26. Does Alpine initialize HTML inserted by HTMX?
Yes. Alpine watches the DOM with a MutationObserver, so new x-data elements in swapped HTML are initialized automatically. A component whose own element is replaced loses its state, so swap the content inside it rather than the component root if state must survive.
Security, testing and choosing
27. What are the security concerns with HTML over the wire?
The same as any server-rendered page: escape all dynamic output to prevent XSS, protect state-changing requests with CSRF tokens (for example hx-headers with the token), authorize every fragment endpoint (the URL is public even if no link points to it), and use a Content-Security-Policy. Alpine's standard build evaluates expressions with new Function, so a strict CSP needs the CSP build of Alpine. See Server-Side Setup.
28. How do you test an HTMX application?
Mostly on the server: a request with HX-Request: true returns the right fragment, status and headers, and ordinary feature tests cover it. Add a few browser tests (Playwright, Cypress) for flows where attributes and the DOM interact, such as targets and swaps. Alpine components can be tested in a browser or with a DOM library, since they are just HTML with attributes.
29. When would you choose HTMX and Alpine over React or Vue?
When the app is mostly server-rendered documents with islands of interactivity: admin panels, dashboards, content sites, internal tools, and teams with strong backend skills. There is no build step, less JavaScript to ship, and the server stays the single source of truth. Choose a SPA framework for offline support, very rich client-side state, heavy real-time editing or when a mobile app shares the same JSON API.
30. What are the downsides of this approach?
Every interaction is a network round trip (latency and server load), complex client-side state is awkward, the attribute-heavy HTML can become hard to read, there is less ecosystem tooling than React, and debugging means following the network tab. Mitigate with partial templates, small fragments and clear naming conventions.
Predict the output
Puzzle 1: Alpine scope
What does the page show after you press the button twice? Decide first, then run it.
The inner scope can read and write the parent's n, so after two presses the outer line shows 3 and the inner line shows 15 (3 + 12). Nested scopes inherit; they are not isolated.
Puzzle 2: one response, two places
Press the button. Which two elements change, and why?
The <li> goes to the normal target (#list, appended), and the <span id="badge" hx-swap-oob="true"> is pulled out of the response and replaces the element with the same id.
Summary
- HTMX: any element, any method, any event, any target, driven by attributes; swaps are
innerHTMLby default; only 2xx responses swap. - Alpine: reactive state in
x-data, one-wayx-bind, two-wayx-model,x-showversusx-if,$dispatch,Alpine.dataversusAlpine.store. - Together: HTMX for server state, Alpine for UI state; Alpine initializes swapped HTML automatically.
- Safe use means escaping output, CSRF tokens, authorizing fragment endpoints and a CSP.
- Be ready to explain trade-offs: latency and server load versus simplicity and less JavaScript.
Next: Cheat Sheet
Track your progress
Create a free account to tick off lessons and pick up where you left off on any device.
of HTMX and Alpine.js lessons completed