htmx icon indicating copy to clipboard operation
htmx copied to clipboard

[proposal] hx-reset option to reset form after triggered

Open tdb-alcorn opened this issue 2 years ago • 6 comments

This is a feature proposal to add a new hx-reset field available on

s that would call form.reset() after the form's htmx behavior has been triggered (presumably, submit). In the use case where multiple form submissions are expected in sequence, this would obviate the need to send back an empty version of the form after receiving each submission. It would also preserve the active element and feel snappier in general.

The default behavior would be the same as now, that is without the option present, no reset would occur.

Thoughts?

tdb-alcorn avatar Apr 16 '23 03:04 tdb-alcorn

Hm, what about something along hx-on "htmx:afterRequest if e.detail.successful then reset next form"?

Since you are sending the form and await an answer, you could also just resend the plain form, that’s not much overhead. You could also send a status message out-of-band with that response.

An extension to handle the reset by using a new attribute should be easy to create though.

andryyy avatar Apr 16 '23 06:04 andryyy

+1, sending the whole form back over the wire seems like overkill and having to implement your own extension as an alternative is not ideal. If I can find some time I may be able to work on this.

jakespracher avatar Jul 25 '23 20:07 jakespracher

Hi, I gave this one a go; the behavior is described in the PR description and through the tests. Hope this is what was intended for a hx-reset attribute.

matiboy avatar Jul 26 '23 02:07 matiboy

hx-on::after-request="this.reset()"

This way you can reset the form after submitting it.

Example for Django (Python):

<form hx-post="{% url 'comment_add' post.id %}"
    hx-target="#tab-contents"
    hx-swap="afterbegin"
    hx-on::after-request="this.reset()">
    {% csrf_token %}
    {{ form }}
    <button type="submit">Отправить</button>
</form>

electroae4 avatar Mar 18 '24 20:03 electroae4