Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 28 ยท Practice and Review

Cheat Sheet

One-page HTMX and Alpine.js cheat sheet: attributes, triggers, swap values, headers, events, directives, magics and plugins, with two quick live checks.

This page is a quick lookup for everything in the track: the HTMX attributes, triggers, swap styles, headers and events, and the Alpine.js directives, magic properties and plugins. It is a reference, so it explains little. For the reasoning behind each entry, follow the links to the lessons.

Versions: htmx 2.0.x and Alpine.js 3.x (the examples here pin htmx 2.0.4 and Alpine 3.14.8).

Two libraries, one page

HTMX Talks to the server, swaps HTML.
Alpine Local state and small behaviors.
1 / 2

HTMX attributes

Requests

Attribute What it does
hx-get, hx-post, hx-put, hx-patch, hx-delete Send that HTTP request to the URL when triggered
hx-trigger Which event starts the request (see triggers below)
hx-target Element that receives the response
hx-swap How the response is inserted
hx-select Pick part of the response by CSS selector
hx-swap-oob Swap this response element into the element with the same id
hx-vals Extra values as JSON, for example hx-vals='{"page": 2}'
hx-include Include values of other elements in the request
hx-params Filter parameters: *, none, not name, or a list
hx-headers Extra request headers as JSON
hx-encoding Use multipart/form-data for file uploads
hx-sync Coordinate overlapping requests, for example this:replace

Feedback and navigation

Attribute What it does
hx-indicator Element that gets the htmx-request class during a request
hx-disabled-elt Elements to disable while the request runs
hx-confirm Ask the user to confirm before sending
hx-push-url Push the URL into history (true, false or a path)
hx-replace-url Replace the current history entry
hx-boost Turn links and forms inside into AJAX navigation
hx-history, hx-history-elt Control history snapshots
hx-preserve Keep this element unchanged across swaps (needs an id)
hx-on:event Inline JavaScript for an event, for example hx-on:click="..." or hx-on::after-request
hx-disable Ignore HTMX attributes inside this element
hx-ext Enable an extension

Triggers

Trigger Meaning
click, submit, change, input, keyup Any DOM event (the default is the natural event of the element)
load When the element is loaded
revealed When it scrolls into view the first time
intersect When it intersects the viewport (threshold:0.5)
every 5s Poll at an interval
keyup[key=='Enter'] Event filter in square brackets
Modifier Meaning
once Only the first time
changed Only if the value changed
delay:300ms Wait for quiet before sending
throttle:1s At most once per interval
from:selector Listen on another element (from:document, from:#field)
target:selector Only when the event target matches
consume Stop the event from bubbling
queue:first, queue:last, queue:all, queue:none What to do with events during a request

Swap values and modifiers

Value Result
innerHTML Replace the target's content (default)
outerHTML Replace the target itself
beforebegin Insert before the target
afterbegin Insert as the first child
beforeend Insert as the last child
afterend Insert after the target
delete Remove the target, ignoring the response
none Do not swap (out-of-band content still processed)

Add modifiers after the value: hx-swap="outerHTML swap:300ms settle:200ms scroll:top show:window:top transition:true".

hx-target selectors

this, closest <css>, find <css>, next <css>, previous <css>, or any CSS selector such as #results.

HTMX headers

Request headers Meaning
HX-Request Always true for HTMX requests
HX-Target, HX-Trigger, HX-Trigger-Name Ids and name of the target and trigger elements
HX-Current-URL The page URL
HX-Boosted Request came from a boosted link or form
HX-Prompt The answer from hx-prompt
Response headers Meaning
HX-Redirect Client-side redirect to a URL
HX-Location Navigation without a full reload
HX-Refresh true reloads the page
HX-Push-Url, HX-Replace-Url Update the history
HX-Retarget, HX-Reswap, HX-Reselect Override target, swap and selection
HX-Trigger, HX-Trigger-After-Swap, HX-Trigger-After-Settle Fire client events, optionally JSON like {"saved": {"id": 3}}

HTMX events and classes

Event When
htmx:configRequest Before sending: change headers and parameters
htmx:beforeRequest, htmx:afterRequest Around the request
htmx:beforeSwap Decide whether and how to swap (shouldSwap, target)
htmx:afterSwap, htmx:afterSettle After insertion and after settling
htmx:load New content was processed
htmx:responseError, htmx:sendError, htmx:targetError Failure cases
htmx:historyRestore History was restored
Class Added Use
htmx-request During the request Spinners
htmx-swapping Before the swap Fade-out
htmx-settling After the swap Fade-in
htmx-added On new content for one tick Entry animation

JavaScript API: htmx.ajax(verb, url, target), htmx.process(el), htmx.on(event, fn), htmx.trigger(el, name), htmx.logAll(), htmx.config.

Alpine.js directives

Directive What it does
x-data Declare a component and its state
x-init Run code when the component starts
x-text, x-html Set text (safe) or HTML (unsafe with user data)
x-show Toggle display: none
x-if Add or remove a <template> (one root element)
x-for Loop over a list on a <template> (item in items, add :key)
x-bind:attr or :attr Bind an attribute; :class and :style take objects
x-on:event or @event Listen to an event
x-model Two-way binding (.number, .trim, .lazy, .debounce, .fill)
x-ref Name an element for $refs
x-effect Re-run an expression when its dependencies change
x-transition Enter and leave animations
x-cloak Hide until Alpine is ready (needs a CSS rule)
x-ignore Skip Alpine inside this element
x-teleport Move a template's content to another place (modals)
x-id Generate scoped ids with $id()
x-modelable Expose an inner value to x-model

Event modifiers

.prevent, .stop, .self, .once, .passive, .capture, .window, .document, .outside, .debounce, .throttle, .camel, .dot. Key filters: @keydown.enter, .escape, .space, .arrow-up, .shift, .ctrl, .meta, combinations like @keydown.shift.enter.

Magic properties

Magic Returns
$el The current element
$refs Elements marked with x-ref
$event The native event
$dispatch('name', detail) Fire a custom event
$watch('prop', (new, old) => ...) Run code on change
$nextTick(fn) Run after the DOM updates
$store Global stores
$data The component's data object
$root The nearest x-data element
$id('name') A unique id

Globals and plugins

API Purpose
Alpine.data(name, fn) Reusable component definition
Alpine.store(name, obj) Global shared state
Alpine.bind(name, fn) Reusable attribute bundles
Alpine.start() Start Alpine (npm installs)
alpine:init Event fired before Alpine starts

Official plugins: Persist ($persist), Focus (x-trap), Collapse (x-collapse), Mask (x-mask), Intersect (x-intersect), Anchor (x-anchor), Morph, Sort (x-sort). Each plugin script must load before the Alpine core script.

Two quick live checks

First, an Alpine cheat in action: a dropdown that uses x-data, @click.outside, x-show and x-transition.

Alpine in six lines

Second, an HTMX cheat in action: hx-trigger="every 2s" with hx-swap="innerHTML", driven by the mock server from Setting Up. The counter increases on its own.

HTMX polling in one attribute

Quick decision table

You need Use
Show or hide something, toggle a class, track a form draft Alpine
Load, save or delete data on the server HTMX
Update a badge from a list response HTMX hx-swap-oob
Close a menu on outside click Alpine @click.outside
Search while typing HTMX hx-trigger="input changed delay:300ms"
Share state between components Alpine.store or $dispatch events
React to a server event in the UI HX-Trigger header plus an Alpine @event.window listener

Summary

  • HTMX attributes: verbs, hx-trigger, hx-target, hx-swap (default innerHTML), hx-vals, hx-include, hx-indicator, hx-push-url, hx-confirm, hx-boost, hx-on.
  • HTMX headers and events: HX-Request and the HX-* response headers; htmx:configRequest, htmx:beforeSwap, htmx:afterSwap.
  • Alpine directives: x-data, x-text, x-show, x-if, x-for, x-bind, x-on, x-model, x-ref, x-effect, x-transition, x-cloak.
  • Magic properties start with $; reusable code goes in Alpine.data and Alpine.store.
  • Practice with the exercises, or start again from the introduction.