| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Reset on Submit" |
| 4 | description: Clear form inputs after submission |
| 5 | icon: "icon-[mdi--eraser]" |
| 6 | --- |
| 7 | |
| 8 | <script> |
| 9 | var _replyIndex = 0; |
| 10 | |
| 11 | server.get("/demo", () => { |
| 12 | const inputCls = "flex-1 px-3 py-2 rounded-lg border border-neutral-200 dark:border-neutral-850 bg-white dark:bg-neutral-900 text-sm text-neutral-800 dark:text-neutral-200 placeholder:text-neutral-400 dark:placeholder:text-neutral-500 outline-none"; |
| 13 | const btnCls = "px-4 py-2 text-sm font-semibold rounded-lg text-white bg-neutral-800 dark:text-neutral-900 dark:bg-neutral-200 cursor-pointer interact:bg-neutral-700 dark:interact:bg-neutral-300 active:scale-[0.98] transition"; |
| 14 | return ` |
| 15 | <div class="flex flex-col h-[300px]"> |
| 16 | <div id="messages" class="flex-1 overflow-y-auto space-y-3 pb-3 pr-3 scrollbar-subtle"></div> |
| 17 | <form class="flex gap-2 shrink-0 mt-3" |
| 18 | hx-post="/chat" hx-target="#messages" hx-swap="beforeend" |
| 19 | hx-on:htmx:after:request="this.reset()" |
| 20 | hx-on:htmx:after:swap="document.getElementById('messages').scrollTop = document.getElementById('messages').scrollHeight"> |
| 21 | <input type="text" name="message" placeholder="Send a messageā¦" required autocomplete="off" |
| 22 | class="${inputCls}"> |
| 23 | <button class="${btnCls}">Send</button> |
| 24 | </form> |
| 25 | </div>`; |
| 26 | }); |
| 27 | |
| 28 | server.post("/chat", (req) => { |
| 29 | const replies = [ |
| 30 | "have you tried just returning HTML", |
| 31 | "look, just use a form. forms have been working since 1993", |
| 32 | "you're mass producing artisanal web apps now. welcome to the club", |
| 33 | "hypermedia is how the web was meant to work. we just forgot for a bit", |
| 34 | "the browser already knows how to do this. we just had to get out of its way", |
| 35 | ]; |
| 36 | const msg = req.params["message"]; |
| 37 | if (!msg) return; |
| 38 | const reply = replies[_replyIndex++ % replies.length]; |
| 39 | return ` |
| 40 | <div class="starting:opacity-0 starting:translate-y-1 transition duration-300 ease-out flex justify-end"> |
| 41 | <div class="rounded-lg rounded-br-sm bg-blue-600 dark:bg-blue-500 px-3.5 py-2 text-sm text-white max-w-[75%]">${msg}</div> |
| 42 | </div> |
| 43 | <div class="starting:opacity-0 starting:translate-y-1 flex" style="transition:opacity 300ms ease-out 400ms,transform 300ms ease-out 400ms"> |
| 44 | <div class="rounded-lg rounded-bl-sm bg-neutral-100 dark:bg-neutral-900 px-3.5 py-2 text-sm text-neutral-700 dark:text-neutral-300 max-w-[75%]">${reply}</div> |
| 45 | </div>`; |
| 46 | }); |
| 47 | |
| 48 | server.start("/demo"); |
| 49 | </script> |
| 50 | |
| 51 | <div id="demo-content" class="not-prose demo-container min-h-[380px]"></div> |
| 52 | |
| 53 | ## Basic usage |
| 54 | |
| 55 | On the client, wrap your inputs in a `<form>` and use [`hx-on`](/reference/attributes/hx-on) to reset it after each successful request. |
| 56 | |
| 57 | ```html |
| 58 | <form hx-post="/chat" |
| 59 | hx-target="#messages" |
| 60 | hx-swap="beforeend" |
| 61 | hx-on:htmx:after:request="this.reset()"> |
| 62 | <input type="text" name="message"> |
| 63 | <button>Send</button> |
| 64 | </form> |
| 65 | <div id="messages"></div> |
| 66 | ``` |
| 67 | |
| 68 | - [`hx-post`](/reference/attributes/hx-post) submits the form to `/chat`. |
| 69 | - [`hx-target`](/reference/attributes/hx-target) points at the `#messages` container. |
| 70 | - [`hx-swap`](/reference/attributes/hx-swap)=[`"beforeend"`](/reference/attributes/hx-swap#beforeend) appends each new message to the bottom. |
| 71 | - [`hx-on:htmx:after:request`](/reference/attributes/hx-on) listens for the [`htmx:after:request`](/reference/events/htmx-after-request) event and calls `this.reset()` to clear the form. |
| 72 | |
| 73 | On the server, respond with the new message: |
| 74 | |
| 75 | ```html |
| 76 | <div class="message user">Hello!</div> |
| 77 | <div class="message bot">Hi there! How can I help?</div> |
| 78 | ``` |
| 79 | |
| 80 | ## Notes |
| 81 | |
| 82 | ### Without a form element |
| 83 | |
| 84 | The `reset()` method is only available on `<form>` elements. For standalone inputs, select the element and clear its value directly: |
| 85 | |
| 86 | ```html |
| 87 | <input id="chat-input" type="text" name="message"> |
| 88 | <button hx-post="/chat" |
| 89 | hx-target="#messages" |
| 90 | hx-swap="beforeend" |
| 91 | hx-include="#chat-input" |
| 92 | hx-on:htmx:after:request="document.getElementById('chat-input').value = ''"> |
| 93 | Send |
| 94 | </button> |
| 95 | ``` |
| 96 | |
| 97 | - [`hx-include`](/reference/attributes/hx-include) tells the button which input to include in the request, since it is outside the button element. |