Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2022.
* Some parts of this feature may have varying levels of support.
The HTMLDialogElement interface provides methods to manipulate <dialog> elements. It inherits properties and methods from the HTMLElement interface.
EventTarget Node Element HTMLElement HTMLDialogElementAlso inherits properties from its parent interface, HTMLElement.
HTMLDialogElement.closedByA string that sets or returns the closedby HTML attribute, indicating the types of user actions that can be used to close the dialog.
HTMLDialogElement.openA boolean value reflecting the open HTML attribute, indicating whether the dialog is available for interaction.
HTMLDialogElement.returnValueA string that sets or returns the return value for the dialog.
Also inherits methods from its parent interface, HTMLElement.
HTMLDialogElement.close()Closes the dialog. An optional string may be passed as an argument, updating the returnValue of the dialog.
HTMLDialogElement.requestClose()Requests to close the dialog. An optional string may be passed as an argument, updating the returnValue of the dialog.
HTMLDialogElement.show()Displays the dialog modelessly, i.e., still allowing interaction with content outside of the dialog.
HTMLDialogElement.showModal()Displays the dialog as a modal, over the top of any other dialogs that might be present. Everything outside the dialog is inert with interactions outside the dialog being blocked.
Also inherits events from its parent interface, HTMLElement.
Listen to these events using addEventListener() or by assigning an event listener to the oneventname property of this interface.
cancelFired when the dialog is requested to close, whether with the escape key, or via the requestClose() method. If the event is canceled (via Event.preventDefault()), the dialog will remain open. If not canceled, the dialog will close and the close event will be fired.
closeFired when the dialog is closed.
The following example shows a button that, when clicked, uses the showModal() function to open a modal dialog containing a form.
While open, everything other than the modal dialog's contents is inert. You can click the Close button to close the dialog (via the close() function), or submit the form via the Confirm button.
The example demonstrates:
The code first gets objects for the <dialog> element, the <button> elements, and the <select> element. It then adds a listener to call the HTMLDialogElement.showModal() function when the Open Dialog button is clicked.
Next we add a listener to the Close button click event. The handler set the returnValue and calls the close() function to close the dialog.
Next we add a listener to the <form> submit event. The form is submitted when the required <select> element has a value and the Confirm button is clicked. If the <select> element does not have a value the form will not submit and the dialog will remain open.
Calling close() (or successfully submitting a form with method="dialog"") fires the close event, which we implement below by logging the return value of the dialog.
The cancel event is fired when "platform specific methods" are used to close the dialog, such as the Esc key. It is also fired when the requestClose() method is called. The event is "cancelable" which means that we could use it to prevent the dialog from closing. Here we just treat the cancel as a "close" operation, and reset the returnValue to "" to clear any value that may have been set.
The toggle event (inherited from HTMLElement) is fired just after a dialog has opened or closed (but before the close event).
Here we add a listener to log when the dialog opens and closes.
Note: The toggle and beforetoggle events may not be fired at dialog elements on all browsers. On these browser versions you can instead check the open property after attempting to open/close the dialog.
The beforetoggle event (inherited from HTMLElement) is a cancellable event that is fired just before a dialog is opened or closed. If needed, this can be used to prevent a dialog from showing, or to perform actions on other elements that are affected by the dialog open/close state, such as adding classes on them to trigger animations.
In this case we just log the old and new state.
Try out the example below. Note that both Confirm and Close buttons result in the close event being fired, and that the result should reflect the selected dialog option.
| HTML # htmldialogelement |
| HTML # event-beforetoggle |
| HTML # event-toggle |
Enable JavaScript to view this browser compatibility table.
Enable JavaScript to view this browser compatibility table.
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jan 26, 2026 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.