Under The Hood
frontend htmx HTMX 1.x / 2.x

hx-vals & hx-include: Dynamic JSON and Form Payload Serialization

Last updated
Prerequisites:
HTML Form serialization
JSON syntax in HTML attributes
  • htmx
  • hx-vals
  • hx-include
  • serialization
  • forms
  • payload

Read at your depth

The practical view

`hx-vals` adds extra parameters to an HTMX request: `hx-vals='{"role": "admin", "source": "banner"}'`. For dynamic JavaScript expressions, use `js:` prefix: `hx-vals='js:{timestamp: Date.now()}'`. `hx-include="#other-form, #search-input"` includes the values of external inputs or forms in the current request payload.

The same idea in other frameworks

react equivalent
`const payload = { ...formData, role: 'admin', timestamp: Date.now() }`.

Legacy vs modern

Manual JS Payload Assembly vs Declarative hx-include

Assembling payloads from disconnected inputs in JS requires querySelectors; `hx-include` serializes them declaratively.

before → after
Manual Object Assembly in JS
body: JSON.stringify({ a: inputA.value, b: inputB.value }); // ❌ Manual collection
Declarative hx-include + hx-vals
<button hx-post="/checkout" hx-include="#filters, #coupon" hx-vals='js:{clientTime: Date.now()}'> // ✅ NEW

Interview gotchas

Context

Interviewers test your knowledge of HTTP protocol structure within HTMX.

The mechanical answer

`hx-vals` adds parameters to the **request body** (for POST/PUT) or query string parameters (for GET). `hx-headers` adds custom **HTTP headers** to the request (e.g. `hx-headers='{"Authorization": "Bearer token"}'` or `{"X-Custom-ID": "123"}`).

Trap

Do not pass authentication tokens into `hx-vals` if your backend middleware expects them in the `Authorization` HTTP header.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.