htmx, include hidden input fields outside of a form
By Flavio
Published · Updated
Learn how to send extra data with htmx without a hidden input or a form by using the hx-vals attribute on a button to pass key-value pairs with the request.
Using htmx I had a list of items, think a list of tasks, with task text and more details, and inside this list a “delete button”.
I had the need of sending some information that you’d usually use a hidden input element for.
Something invisible to the user but essential for my app needs.
But I couldn’t use a form for this, I just used a button element, because we can’t nest forms.
Turns out we can use the hx-vals attribute and pass a list of key-value pairs that are going to be added to the request just like a hidden input field would:
<button hx-delete="/item/..." hx-vals='{"key": "value"}' hx-target="#response">
Delete
</button>
I tested everything below with htmx 2 (the current latest version on npm) and htmx 4. I point out where the two behave differently.
Where the values end up on the server
For POST, PUT and PATCH the values go in the request body, like normal form fields.
For GET and DELETE, htmx adds them to the query string instead. This button:
<button hx-delete="/item/3" hx-vals='{"id": "3"}'>Delete</button>
sends DELETE /item/3?id=3. So in Express you read it from req.query, not from req.body:
app.delete('/item/:id', (req, res) => {
console.log(req.query.id) // '3'
res.send('')
})
Also note that every value arrives as a string. If you send {"n": 5, "ok": true}, the server gets '5' and 'true'.
Keep the values flat. Arrays and nested objects are not sent the same way in htmx 2 and htmx 4. In htmx 4, an object value even arrives as the string [object Object].
The JSON must be valid
hx-vals takes JSON, and JSON wants double quotes. Put single quotes around the attribute and double quotes inside:
<!-- works -->
<button hx-post="/done" hx-vals='{"id": "9"}'>Done</button>
<!-- broken -->
<button hx-post="/done" hx-vals="{'id': '9'}">Done</button>
With the broken version, htmx logs a JSON parse error in the browser console. In my tests the request was not sent at all, in both htmx 2 and 4. So if a button suddenly does nothing, check the quotes.
If you need double quotes around the attribute, escape the inner ones as ".
Compute the value with js:
Prefix the value with js: (or javascript:) and htmx evaluates it as JavaScript when the request starts. Inside it, this is the element:
<button
hx-post="/item/done"
data-id="42"
hx-vals='js:{id: this.dataset.id}'>
Done
</button>
This sends id=42.
Be careful here. The expression runs as code, so never build it from text a user typed, or you open the door to XSS.
Often you don’t need any of this. If the value is the item ID, putting it in the URL (hx-delete="/item/42") is the simplest option.
Use an input that lives somewhere else with hx-include
If the value is already in an input on the page, for example a CSRF token, point to it with hx-include and a CSS selector:
<input type="hidden" id="token" name="token" value="abc123">
<ul>
<li>
Buy milk
<button hx-post="/item/3/done" hx-include="#token">Done</button>
</li>
</ul>
The request body now contains token=abc123, even though the input is outside any form.
You can also use closest, which is handy in a list where each row has its own inputs. If the selected element is not an input, htmx includes all the inputs inside it:
<li>
<input name="qty" value="2">
<input type="hidden" name="sku" value="A1">
<button hx-post="/cart" hx-include="closest li">Add</button>
</li>
This sends qty=2&sku=A1.
Put shared values on a parent
In a list, you don’t want to repeat the same value on every button. Put hx-vals on a parent element instead.
In htmx 2, hx-vals is inherited by default. A child with its own hx-vals is merged with the parent’s, and the child wins when both set the same key:
<ul hx-vals='{"listId": "7"}'>
<li>
<button hx-delete="/item/3" hx-vals='{"id": "3"}'>Delete</button>
</li>
</ul>
In htmx 2 this sends both listId and id.
htmx 4 made inheritance explicit. You need hx-vals:inherited on the parent, and a child hx-vals replaces the parent’s values completely. To merge them, use hx-vals:append on the child:
<ul hx-vals:inherited='{"listId": "7"}'>
<li>
<button hx-delete="/item/3" hx-vals:append='{"id": "3"}'>Delete</button>
</li>
</ul>
hx-vals wins over a form input with the same name
If a form has an input named id and the same form also has hx-vals='{"id": "..."}', the server receives the hx-vals value. Pick different names if you need both.
If you find yourself juggling hx-* attributes like these, my free htmx attribute builder generates them from a simple form.
Want me to talk about your product? You can sponsor this site.