HTMLDialogElement: close event
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
The close
event is fired on an HTMLDialogElement
object when the <dialog>
it represents has been closed.
This event is not cancelable and does not bubble.
Syntax
Use the event name in methods like addEventListener()
, or set an event handler property.
js
addEventListener("close", (event) => {});
onclose = (event) => {};
Event type
A generic Event
.
Examples
Live example
HTML
html
<dialog class="example-dialog">
<form method="dialog">
<button>Close via method="dialog"</button>
</form>
<button class="close">Close via .close() method</button>
<p>Or hit the <kbd>Esc</kbd> key</p>
</dialog>
<button class="open-dialog">Open dialog</button>
<div class="result"></div>
JavaScript
js
const result = document.querySelector(".result");
const dialog = document.querySelector(".example-dialog");
dialog.addEventListener("close", (event) => {
result.textContent = "dialog was closed";
});
const openDialog = document.querySelector(".open-dialog");
openDialog.addEventListener("click", () => {
dialog.showModal();
result.textContent = "";
});
const closeButton = document.querySelector(".close");
closeButton.addEventListener("click", () => {
dialog.close();
});
Result
Specifications
Specification |
---|
HTML # event-close |
Browser compatibility
Report problems with this compatibility data on GitHubdesktop | mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
close event |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.