DOM HTMLDialogElement.close() Method
The close() method closes a dialog element and can pass back a value saying how it was closed.
Description
The close() method closes the dialog, optionally setting its returnValue, and fires a close event. A button inside a <form method="dialog"> closes the dialog automatically, using the button's value as the return value, so many dialogs need no script to close.
Calling close() on a dialog that isn't open does nothing. When a modal closes, focus goes back to the element that was focused before it opened, so keyboard users return to where they were.
Listening for the close event, rather than handling each button, lets one function react however the dialog was closed: by a button, by Escape or by close() called from script. Check returnValue to see which choice was made.
Syntax
dialog.close()
dialog.close(returnValue)Parameters
- returnValueOptional. A string to store in the dialog's
returnValue.
Return Value
None (undefined).
Example
Browser Support
The following information will show you the current browser support for the HTMLDialogElement.close() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





