Using hx-disabled-elt on form associated/external submit button #3653
Replies: 1 comment
|
Good question! The root cause is that Here are your cleanest options: Option 1: Move Put the htmx attributes on the button and use <form id="my-form">
<!-- form fields -->
</form>
<button type="submit"
form="my-form"
hx-post="/endpoint"
hx-include="#my-form"
hx-disabled-elt="this">
Submit
</button>Now the button is the request initiator, so Option 2: Use a shared class for multiple buttons If you prefer keeping <form hx-post="/endpoint" hx-disabled-elt=".submit-btn">
<!-- form fields -->
</form>
<button type="submit" form="my-form" class="submit-btn">Submit</button>
<button type="submit" form="my-form" class="submit-btn">Save Draft</button>Option 3: CSS with htmx adds the form.htmx-request ~ button[type="submit"] {
pointer-events: none;
opacity: 0.5;
cursor: not-allowed;
}Note: this does not set the Option 1 is the most idiomatic HTMX approach. Option 2 is best when you need to keep |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I want to disable a submit button when a request is in flight. My submit button is outside the form, but I associate it with the "form" attribute.
However, the hx-disabled-elt doesn't add the disabled attribute when the request is happening. I understand that this button is not technically part of the form yet it is associated with it. I can add hx-disabled-elt to the form and use an id to make sure they're connected
But that feels a bit verbose as I might have several submit buttons for the same button.
I tried a script as well but feels too much hacking into the request flow
So I'm a bit unsure what approach I take best here. What are my options?
All reactions