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.
Theclose event is fired on anHTMLDialogElement object when the<dialog> it represents has been closed.
This event is not cancelable and does not bubble.
In this article
Syntax
Use the event name in methods likeaddEventListener(), or set an event handler property.
js
addEventListener("close", (event) => { })onclose = (event) => { }Event type
A genericEvent.
Examples
>Live example
HTML
html
<dialog> <form method="dialog"> <button>Close via method="dialog"</button> </form> <button>Close via .close() method</button> <p>Or hit the <kbd>Esc</kbd> key</p></dialog><button>Open dialog</button><div></div>button,div { margin: 0.5rem;}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> |