HTML onreset Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

The onreset attribute is an inline event handler for the reset event on a <form> element. It runs when the form is reset to its initial values — typically when the user clicks a <input type="reset"> or <button type="reset"> button, or when your code calls form.reset(). Use it to show confirmation feedback, clear custom preview UI, or reset related widgets that are not form controls. Call event.preventDefault() to cancel the reset. Prefer addEventListener("reset", …) in production.

What You’ll Learn

01

Event handler

Inline JS.

02

reset

Form cleared.

03

<form>

Target element.

04

type="reset"

Trigger button.

05

Custom UI

Clear previews.

06

addEventListener

Preferred way.

Purpose of onreset Attribute

The primary purpose of onreset is to run custom logic when a form returns to its default state — for example showing a “Form cleared” message, wiping a live preview div, or resetting validation styling that the browser does not clear automatically.

Native reset only restores form controls to their initial value, checked, or selected state. Anything outside the form — or custom JavaScript state — may need your handler to clean up.

💡
Cancel with preventDefault

Inside addEventListener("reset", fn), call event.preventDefault() if you need to block the reset — for example after asking the user to confirm.

📝 Syntax

Set onreset on <form>, or assign form.onreset in JavaScript:

onreset.html
<form onreset="handleReset()">
  <input type="text" name="name">
  <input type="reset" value="Reset">
</form>

<script>
  form.onreset = handleReset;
  form.addEventListener("reset", handleReset);
</script>

Syntax Rules

  • Value is JavaScript executed when the reset event fires on the form.
  • Valid on <form> elements only.
  • Triggered by type="reset" buttons or form.reset().
  • Restores controls to values from when the page loaded (or last programmatic set).
  • Use event.preventDefault() in listeners to cancel the reset.
  • Modern alternative: form.addEventListener("reset", handler).
  • Not for individual inputs — attach to the parent form.

💎 Values

The onreset attribute accepts a string of JavaScript code:

  • onreset="handleReset()" — Call a named function.
  • onreset="status.textContent = 'Cleared'" — Inline statement.
  • JavaScript: form.onreset = () => { … } — property assignment.
onreset-handler.html
function handleReset(event) {
  status.textContent = "Form has been reset!";
  preview.textContent = "";
  // event.preventDefault(); // uncomment to cancel reset
}

form.addEventListener("reset", handleReset);

⚡ Quick Reference

Property / eventWhen it firesNotes
resetForm reset requestedonreset
submitForm submittedonsubmit
type="reset"Reset button clickInside the form
form.reset()Programmatic resetAlso fires event
event.preventDefault()Inside reset handlerCancel the reset

Applicable Elements

ElementSupported?Notes
<form>YesPrimary target
<input type="reset">TriggerNot the handler target
<button type="reset">TriggerSame as input reset
<input>, <div>NoUse form-level handler
form.reset()RelatedJS API — fires reset event

onreset vs onsubmit vs form.reset()

APIWhen it firesTypical use
onresetForm cleared to defaultsFeedback, clear custom UI
onsubmitForm submittedValidate, send data
form.reset()When you call itClear after success
input type="reset"User clicks ResetBuilt-in clear button

Examples Gallery

Inline form handler, dynamic assignment, and clearing a custom preview with addEventListener.

👀 Live Preview

Fill the form and click Reset — status updates via reset:

Waiting for reset…

Example — onreset on form

Show feedback when the user clicks Reset:

form-onreset.html
<form onreset="resetForm()">
  <label for="name">Name:</label>
  <input type="text" id="name" name="name">
  <input type="reset" value="Reset">
</form>

<script>
  function resetForm() {
    document.getElementById("status").textContent = "Form has been reset!";
  }
</script>
Try It Yourself

How It Works

Clicking the Reset button fires reset on the form. The inline attribute runs resetForm() so you can react after (or before) fields revert.

Dynamic Values with JavaScript

Assign the handler on the form at runtime:

dynamic-onreset.html
<script>
  document.getElementById("dynamicForm").onreset = function () {
    log.textContent = "This form is being reset!";
  };
</script>
Try It Yourself

How It Works

Assigning form.onreset is equivalent to the inline form attribute. Attach after the form exists in the DOM.

Example — Clear custom preview on reset

Native reset clears inputs but not extra UI — handle that in your listener:

reset-preview.html
form.addEventListener("reset", function () {
  setTimeout(function () {
    preview.textContent = "Preview: (empty)";
  }, 0);
});
Try It Yourself

How It Works

The reset event fires before fields update. A short setTimeout lets you sync custom UI after the browser finishes resetting controls.

♿ Accessibility

  • Label reset buttons clearly — Use text like “Clear form” or “Reset” — not icon-only buttons without aria-label.
  • Announce resets — Use aria-live="polite" on status text when the form is cleared.
  • Confirm destructive clears — If reset loses a lot of work, ask for confirmation before allowing it.
  • Do not remove reset without reason — Users expect a way to start over on long forms.
  • Restore focus — After reset, move focus to the first field or a logical starting point.

🧠 How onreset Works

1

User clicks Reset

Or form.reset().

Trigger
2

reset fires

onreset runs.

Event
3

Fields revert

Default values.

Restore
=

Clean slate

Form + custom UI.

Browser Support

The reset event and onreset handler are supported in all modern browsers, including Internet Explorer 9+.

Form Events · Fully supported

Universal onreset support

All major browsers fire reset on forms when reset is triggered.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onreset attribute 99% supported

Bottom line: Reliable for form reset handling in all modern browsers.

💡 Best Practices

✅ Do

  • Put onreset on the <form>
  • Clear custom preview / validation UI in the handler
  • Use addEventListener("reset", …) in production
  • Confirm before reset on long forms with unsaved work
  • Provide visible feedback when the form is cleared

❌ Don’t

  • Use alert() for every reset
  • Put onreset on individual inputs
  • Assume reset clears JavaScript-only state automatically
  • Hide reset buttons without an alternative clear action
  • Rely on reset to undo server-side saved data

Conclusion

The onreset attribute runs JavaScript when a form returns to its default values — useful for feedback and cleaning up custom UI that native reset does not touch.

Attach it to the form, pair with type="reset" buttons, use event.preventDefault() when you need to block a reset, and prefer addEventListener in production.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onreset

Bookmark these before wiring form reset logic.

5
Core concepts
🔄 02

type="reset"

Trigger.

Button
🚫 03

preventDefault

Cancel.

API
👀 04

Custom UI

Clear extra.

Pattern
05

Not submit

Clear only.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the reset event fires on a form — when the user clicks Reset or form.reset() is called.
Form controls revert to their initial values when the page loaded. Custom divs, previews, or JS state outside the form are not cleared unless your handler does it.
Yes. Call event.preventDefault() in a reset event listener to cancel the reset.
onreset clears the form to defaults. onsubmit runs when the user submits data — opposite actions.
form.addEventListener("reset", handler) is preferred — you need it for preventDefault() and multiple listeners.
Yes in all modern browsers; Internet Explorer 9+.

Handle form reset cleanly

Practice the onreset attribute with inline handlers and custom preview clearing in the Try It editor.

Try onreset demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful