htmx, include hidden input fields outside of a form
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 our 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 wrote 21 books to help you become a better developer:
- HTML Handbook
- Next.js Pages Router Handbook
- Alpine.js Handbook
- HTMX Handbook
- TypeScript Handbook
- React Handbook
- SQL Handbook
- Git Cheat Sheet
- Laravel Handbook
- Express Handbook
- Swift Handbook
- Go Handbook
- PHP Handbook
- Python Handbook
- Linux Commands Handbook
- C Handbook
- JavaScript Handbook
- Svelte Handbook
- CSS Handbook
- Node.js Handbook
- Vue Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025