I know that form.reset() will reset all form fields to their default values, but how does that work?
Is it the browser’s DOM implementation? i.e. the browser knows what were the last values sent from the server in last postback/get and when reset() is called the browser resets those values.
The DOM spec tells us that it:
And so off to HTML. According to the latest spec (still a draft!):
And the "reset algorithm" for
inputelements (for instance) is:So basically,
resetsets the value of an input to the current value of its "value" attribute (theElement.getAttribute("value")), which may be different from its currentvalueproperty (theElement.value). Live example here.Edit: Oooh, Pekka points us at
defaultValue. Very cool, I’d much rather use that thangetAttribute("value"). Revised live example.