Favorites

HTML onclose Event Attribute

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
onclose
HTML Event AttributeThis event attribute is supported by all modern browsers.
Updated 22nd November 2026

Description

The onclose event attribute runs a script when a <dialog> element has been closed, in any way: with the close() or requestClose() methods, with a form that uses method="dialog", with a button that has command="close", or by the user pressing Esc.

The event is not cancelable; it fires after the dialog is already closed. Read dialog.returnValue in the handler to find out how the dialog was closed, for example which button of a method="dialog" form was used. To stop a dialog from being dismissed, use oncancel instead.

<dialog id="confirm" onclose="console.log(this.returnValue)">
  <form method="dialog">
    <button value="yes">Yes</button>
    <button value="no">No</button>
  </form>
</dialog>

Syntax

<element onclose="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
<html>
<head>
<title>onclose event</title>
</head>
<body>
<button onclick="document.getElementById('dlg').showModal()">Open dialog</button>
<dialog id="dlg" onclose="log('close: returnValue = ' + this.returnValue)">
<p>Press Esc or click Close.</p>
<button onclick="this.closest('dialog').close('closed')">Close</button>
</dialog>
<ul id="log"></ul>
<script>
function log(text) {
var li = document.createElement("li");
li.textContent = text;
document.getElementById("log").appendChild(li);
}
</script>
</body>
</html>
Open in editor

Browser Support

The following information will show you the current browser support for the HTML onclose event attribute. Hover over a browser icon to see the version that first introduced support for this HTML event attribute.

This event attribute is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee