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>
- A click sends
POST /order/12/statuswith the bodystatus=Ironing. - Its value is JSON, and JSON requires double quotes (
"). So the attribute is wrapped in single quotes ('). - Numbers are fine:
{"id": 42}sendsid=42. - In a GET request, the values go into the URL:
<button hx-get="/order" hx-vals='{"page": 2}'>sendsGET /order?page=2. hx-valsis inherited by the elements inside. When keys clash, the nearest one wins.
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) |
not a,bsends everything except the ones named.a,bsends only the ones named.nonesends nothing at all.- This filter runs last, after
hx-valsis merged in. Sohx-params="name,weight"also dropsservicefromhx-vals, andnonedrops everything. hx-paramsisn't a safeguard. Anyone can send their own request from DevTools. The server still has to work out the price itself and ignoreestimate.
js: values from JavaScript, and the security note
<button hx-post="/order/12/status" hx-vals='js:{hour: new Date().getHours()}'>
- The
js:prefix makes its content run as JavaScript when the request is built. At 09:15, the body ishour=9. - Because it runs, never put text from users inside
js:, such as a customer's name from the database. Text holding code would run too, and that's an XSS hole. Plain JSON withoutjs:is only read, never run. - Sites with a strict Content Security Policy turn this off with
htmx.config.allowEval = false.js:values are then skipped, and htmx fireshtmx:evalDisallowedError.
⚠️ Quotes the wrong way round, and values that get overwritten
- Quotes the wrong way round.
hx-vals="{'status': 'Ironing'}"isn't valid JSON. htmx writes an error in the Console, and the request isn't sent at all. Single quotes outside, double quotes inside. - Forgetting that
hx-valsoverrides fields. A key that matches an input's name replaces its value without warning. hx-params="none"together withhx-vals. Nothing is sent, thehx-valsvalues included.- Forgetting a value in the exercise. The button needs
hx-post="/api/action",hx-vals='{"action": "approve", "id": 42}', and anhx-target="#result"pointing at an element that exists.
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.