Form Reset Events and State Management
Handle form reset and track field state across user interactions.
Form Reset Events and State Management is a free HTML Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The reset Event
Calling form.reset() or clicking <button type="reset"> fires a reset event on the form. Each field reverts to its initial value (the value attribute, or empty for fresh inputs). Listen for the event to react to the reset programmatically.
Default Values
The initial state used by reset is the defaultValue, defaultChecked or defaultSelected property — not the current value. These reflect the value/checked/selected attributes in the HTML, set at parse time and unchanged by user interaction.
Updating Defaults Programmatically
To make a current value the new "default" (so subsequent resets revert to it), set input.defaultValue = input.value. Useful after a successful save: future resets revert to the saved state rather than the original empty form.
form.addEventListener("submit", () => {
// After save, treat current as defaults
Array.from(form.elements).forEach((el) => {
if ("defaultValue" in el) el.defaultValue = el.value;
});
});Listening to reset
The reset event fires before values revert. To inspect the post-reset state, defer to the next microtask: form.addEventListener("reset", () => queueMicrotask(() => { ... use new values ... })). This is a common confusing detail of the event timing.
Preventing Reset
If you call e.preventDefault() in a reset event handler, the reset is cancelled — values stay as they were. Useful for "are you sure?" confirmations on forms with significant user input.
Dirty State Tracking
Compare current values to defaults to know if the form is dirty: const dirty = Array.from(form.elements).some(el => el.value !== el.defaultValue). Use to enable/disable a save button or to show an unsaved-changes confirmation.
Beforeunload Confirmation
To warn the user before navigating away from a dirty form: window.addEventListener("beforeunload", (e) => { if (isDirty(form)) { e.preventDefault(); } }). Browsers show their generic "leave site?" prompt; custom messages are no longer supported in modern browsers.
Reset Buttons Are Usually Bad UX
Reset buttons reset every field with one accidental click, often catastrophically. Most modern UIs omit them entirely or hide them behind a confirmation. If you need reset, place it far from the submit button and consider a one-step undo.
Form State Persistence
For long forms, save state to localStorage on input events and restore on load. Pair with a "Restore draft?" prompt so users do not lose work to accidental tab closures. Combine with reset/submit handlers to clear saved state when no longer needed.
form.addEventListener("input", () => {
localStorage.setItem("form-draft", JSON.stringify(new FormData(form)));
});Custom Components and reset
Custom form elements (built as web components) participate in form reset via ElementInternals: in your formResetCallback method, restore the default value. Required when the custom element wraps an internal input that needs explicit reset.
Form-Associated Custom Elements
Declaring static formAssociated = true on a custom element class and using this.attachInternals() exposes formResetCallback, formStateRestoreCallback, formDisabledCallback and formAssociatedCallback for integrating with the form lifecycle.
Knowledge Check
What value does each field revert to when form.reset() is called?
Summary
The reset event fires when form.reset() runs; fields revert to defaultValue/defaultChecked/defaultSelected. Update defaults after save to make them the new reset state. Track dirty state by comparing current to default values, persist to localStorage for long forms, and use beforeunload for navigation warnings. Custom elements integrate via formAssociated and formResetCallback.
Frequently asked questions
Is the “Form Reset Events and State Management” lesson free?
Yes — the full text of “Form Reset Events and State Management” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Form Reset Events and State Management”?
Handle form reset and track field state across user interactions. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Form Reset Events and State Management” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The FormData API
- The Constraint Validation API
- reportValidity and setCustomValidity
- Form Reset Events and State Management