Favorites

DOM HTMLDialogElement.close() Method

☕
Enjoying this site?

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

Buy us a coffee
HTMLDialogElement.close()
DOM HTMLDialogElement MethodThis method is supported by all modern browsers.
Updated 28th November 2026

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

<dialog id="dlg"><p>This closes itself after half a second.</p></dialog>
const dlg = document.getElementById('dlg');
dlg.addEventListener('close', () => console.log('returnValue:', dlg.returnValue));
dlg.show();
setTimeout(() => dlg.close('timeout'), 500);

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.

This method 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