Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document form reset pattern #1698

Open
alexpetros opened this issue Aug 17, 2023 · 3 comments
Open

Document form reset pattern #1698

alexpetros opened this issue Aug 17, 2023 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@alexpetros
Copy link
Collaborator

alexpetros commented Aug 17, 2023

<form hx-put="/user" hx-on::after-request="this.reset()">
...
</form>

Oh that does look nice. For what it’s worth: I was not able to figure this out as a succinct alternative. Most suggestions on the internet are to either send a whole new form over the wire or to write more js than that. For example:

https://stackoverflow.com/questions/70043237/how-do-we-make-a-htmx-response-trigger-a-form-reset

If you choose to reject this PR on the grounds that the above is sufficient, it might be worth calling that out in the docs somewhere as this seems like a common pattern. It was the very first dilemma I ran into when trying out HTMX.

Originally posted by @jakespracher in #1636 (comment)

@alexpetros alexpetros added the documentation Improvements or additions to documentation label Aug 17, 2023
@alexpetros
Copy link
Collaborator Author

This should probably have it's own example in the examples section, like #1670

@heinrichgh
Copy link

heinrichgh commented Sep 3, 2023

Coming from #1636 after needing the same thing, I would like to place emphasis on the "after successful request".

It can still be achieved with the existing syntax, its just slightly more verbose:

<form hx-put="/user" hx-on::after-request=" if(event.detail.successful) this.reset()">
...
</form>

I believe the majority use case would be to reset a form only after a successful request, therefore documenting the example as such might be beneficial.

@tedyoung
Copy link

tedyoung commented Mar 4, 2024

I would've liked to have seen this as an example within the hx-on reference, or at least a link. Just spent some time coming to the same solution using the event.detail as mentioned above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants