Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onreset — handling form resets

The onreset attribute contains JavaScript that handles a form's reset event. The handler runs before default values are restored and can cancel the reset.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onreset="JavaScript code"

onreset is an HTML event handler attribute for the reset event. Its value is JavaScript code, not an event name or a Boolean value. It is usually placed on a form; the event bubbles, so an ancestor can also handle it.

Resetting through a type="reset" button or form.reset() first dispatches a cancelable reset event at the form. Only afterward are controls restored to their default state—not necessarily empty values. The handler can still read the values from before the reset.

Inside the attribute code, event refers to the event and this to the handling element. event.preventDefault() or return false cancels the reset. Returning false alone does not stop propagation.

The onreset attribute and JavaScript property form.onreset configure the same single handler. Alternatively, form.addEventListener('reset', handler) allows multiple handlers to be registered independently. CSP can block attribute code; the example requires permission for it to execute.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onreset example</title>
<form id="demo" onreset="document.getElementById('log').textContent += 'during reset: ' + this.elements.namedItem('nickname').value + '\n';">
  <label>Nickname <input name="nickname" value="Ada"></label>
  <button type="reset">Reset</button>
</form>
<pre id="log"></pre>
<script>
  const form = document.getElementById('demo');
  const field = form.elements.namedItem('nickname');
  field.value = 'Grace';
  form.reset();
  document.getElementById('log').textContent += 'after reset: ' + field.value;
</script>
</html>

Result

during reset: Grace
after reset: Ada

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources