HTMLDialogElement

The HTMLDialogElement interface provides methods to manipulate

elements. It inherits properties and methods from the HTMLElement interface.

EventTarget Node Element HTMLElement HTMLDialogElement

Instance properties

Also inherits properties from its parent interface, HTMLElement.

HTMLDialogElement.closedBy Experimental

A string that sets or returns the closedby attribute value of the

element, which indicates the types of user actions that can be used to close the dialog.

HTMLDialogElement.open

A boolean value reflecting the open HTML attribute, indicating whether the dialog is available for interaction.

HTMLDialogElement.returnValue

A string that sets or returns the return value for the dialog.

Instance methods

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 are inert with interactions outside the dialog being blocked.

Events

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.

cancel

Fired when the dialog is requested to close, whether with the escape key, or via the HTMLDialogElement.requestClose() method.

close

Fired when the dialog is closed, whether with the escape key, the HTMLDialogElement.close() method, or via submitting a form within the dialog with method="dialog".

Examples

Opening a modal dialog

The following example shows a button that, when clicked, uses the HTMLDialogElement.showModal() function to open a modal

containing a form.

While open, everything other than the modal dialog's contents is inert. You can click the Cancel button to close the dialog (via the HTMLDialogElement.close() function), or submit the form via the Confirm button.

The example demonstrates how you might use all the "state change" events that can be fired on the dialog: cancel and close, and the inherited events beforetoggle, and toggle.

HTML

html


  

JavaScript

Showing the dialog

The code first gets objects for the