htmx
htmx copied to clipboard
[proposal] hx-reset option to reset form after triggered
This is a feature proposal to add a new hx-reset field available on
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.
+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.
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.
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>