hx-vals and hx-params — HTMX

hx-vals='{"status": "Ironing"}' adds JSON values to a request: into the body for POST, into the URL for GET (?page=2). Wrap it in single quotes, because JSON…

The "Mark ironing" button on the order board has no input box, yet the server still needs to know the new status. The opposite happens a lot too: the form has a "price estimate" box that's only there for the customer to see, and that box must not be sent.

hx-vals adds values to a request as JSON, values that aren't in any input box. hx-params filters which parameters actually get sent. At Mrs. Rini's Laundry counter, both happen on the slip. When a customer picks the express service, Mrs. Rini stamps "EXPRESS" on the slip: a value that isn't written in any box. That's hx-vals. Before the slip goes to the back, she cuts off the "price estimate" strip she showed the customer, because the back works out the price itself. That's hx-params.

hx-vals: extra values as JSON

A button on the order board has no input box, so its data comes with hx-vals:

<span id="status-12">Washing</span>
<button hx-post="/order/12/status" hx-vals='{"status": "Ironing"}' hx-target="#status-12">
  Mark ironing
</button>

In a form: merged with the fields

<form id="intake" hx-post="/order" hx-target="#receipt" hx-vals='{"service": "express"}'>
  <input name="name" value="Mrs. Tini">
  <input name="weight" value="4">
  <input name="estimate" value="$28" readonly>
  <button>Take in laundry</button>
</form>

The body: name=Mrs.%20Tini&weight=4&estimate=%2428&service=express. The values from hx-vals go at the end, after the form's fields.

hx-vals wins over a field with the same name

With hx-vals='{"weight": 10}', the body becomes name=Mrs.%20Tini&estimate=%2428&weight=10. The 4 from the box is dropped, and the server gets 10. htmx removes any field whose name matches an hx-vals key, then puts the hx-vals value at the end. Use it on purpose, or give the key a different name.

hx-params: filter what's sent

hx-params Body sent
* (the default) name=Mrs.%20Tini&weight=4&estimate=%2428
not estimate name=Mrs.%20Tini&weight=4
name,weight name=Mrs.%20Tini&weight=4
none (empty)

js: values from JavaScript, and the security note

<button hx-post="/order/12/status" hx-vals='js:{hour: new Date().getHours()}'>

⚠️ Quotes the wrong way round, and values that get overwritten

TL;DR: hx-vals='{"status": "Ironing"}' adds JSON values to a request: into the body for POST, into the URL for GET (?page=2). Wrap it in single quotes, because JSON needs double quotes, and broken JSON means the request isn't sent. hx-vals values go after the form's fields and override a field with the same name. hx-params filters at the end: * (the default), not estimate, name,weight or none, and it filters hx-vals too. js: runs JavaScript, so never fill it with text from users.

⚡ hx-vals: extra values as JSON

Write:

<span id="status-12">Washing</span>
<button hx-post="/order/12/status" hx-vals='{"status": "Ironing"}' hx-target="#status-12">
  Mark ironing
</button>
Guess first: the quotes are turned around, hx-vals="{'status': 'Ironing'}". What happens when the button is clicked?

The request isn't sent at all. That isn't valid JSON, and htmx only writes an error in the Console.

Meaning: A click sends the body status=Ironing. JSON needs double quotes, so the attribute is wrapped in single quotes.

⚡ hx-vals overrides a field with the same name

Write:

<form id="intake" hx-post="/order" hx-target="#receipt" hx-vals='{"service": "express"}'>
  <input name="name" value="Mrs. Tini">
  <input name="weight" value="4">
  <input name="estimate" value="$28" readonly>
  <button>Take in laundry</button>
</form>

Meaning: The hx-vals values go after the form's fields. When a key matches a field's name, such as {"weight": 10}, the box's value is dropped and the server gets 10.

⚡ hx-params filters at the end

Meaning: not estimate sends everything except the ones named, name,weight only the ones named, and none sends nothing. This filter runs last, so the hx-vals values are filtered too. It isn't a safeguard: the server still has to work out the price itself.

What you will learn