HTML oncancel Event Attribute
Description
The oncancel event attribute runs a script when a <dialog> element is dismissed by the user, for example by pressing the Esc key, or when a <dialog closedby="any"> is closed by clicking outside it. It does not fire when the dialog is closed with the close() method or a form with method="dialog".
The event is cancelable: calling event.preventDefault() keeps the dialog open, which is useful when it holds unsaved data. Browsers may ignore this when the user presses Esc repeatedly without interacting with the page, so that a dialog cannot trap the user.
The cancel event also fires on <input type="file"> elements when the user closes the file picker without choosing a file or without changing the selection.
<dialog oncancel="return confirm('Discard your changes?')">...</dialog>
Syntax
<element oncancel="script">
Values
- scriptThe name of the script to use when the event has been triggered.
Example
Browser Support
The following information will show you the current browser support for the HTML oncancel event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.
Desktop





Tablets & Mobile





