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.
Code
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.
documentexist 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
JSON to Zod Schema ConverterInfer typed Zod z.object schemas from JSON payloads locally, supporting nested objects, merged array shapes, enums, nullable fields, and JSDoc comments.JSON Schema ValidatorValidate JSON data against a JSON Schema definition and get detailed error reports. Supports type checking, required fields, and common constraint validation.JSONL / NDJSON InspectorValidate NDJSON and JSON Lines line-by-line, pinpoint syntax errors by line number, analyze key frequencies and types, and export a pretty JSON array.Credit Card Brand DetectorIdentify the card network from a number's prefix and length, validate the Luhn checksum, and show a masked form and the issuer identification number.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.