reset Event Clears Custom Form State

form.reset() restores controls natively but custom UI state is yours; the reset event runs before the revert, so deferred reads see the restored values.

TypescriptForm Validation

Code

typescriptsnippet
16 lines
const form = document.querySelector('form') as HTMLFormElement;
const countInput = form.querySelector('#count') as HTMLInputElement;
const dirtyFlag = form.querySelector('.dirty-flag') as HTMLElement;

// form.reset() restores controls natively but custom UI state is yours to
// clean up; the reset event runs before the revert, so defer the reads.
form.addEventListener('reset', () => {
  queueMicrotask(() => {
    const reverted = countInput.value === countInput.defaultValue;
    dirtyFlag.hidden = reverted;
    console.log('reverted to default:', reverted);
    console.log('dirty flag hidden:', dirtyFlag.hidden);
  });
});

console.log('default count:', countInput.defaultValue);

The shape of this snippet — reset Event Clears Custom Form State

The file’s state is a handful of top-level bindings: form, countInput, dirtyFlag.

Caveats

  • The file adds no dependency — registries and lockfiles stay untouched.
  • document exist only in browsers — a Node runtime has no shims for them out of the box.

Applying it

Watch the console for these labels: reverted to default:, dirty flag hidden:, default count: — when the file runs.

Statements that matter

form.addEventListener('reset', () => {
  queueMicrotask(() => {
    const reverted = countInput.value === countInput.defaultValue;
    dirtyFlag.hidden = reverted;
    console.log('reverted to default:', reverted);
    console.log('dirty flag hidden:', dirtyFlag.hidden);
  });
});

Interactive Tools for This Topic

FAQ

Does reset Event Clears Custom Form State run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.