W3cubDocs

/DOM

HTMLDialogElement

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.

The HTMLDialogElement interface provides methods to manipulate <dialog> elements. It inherits properties and methods from the HTMLElement interface.

Properties

Inherits properties from its parent, HTMLElement.

HTMLDialogElement.open
Is a Boolean reflecting the open HTML attribute, indicating that the dialog is available for interaction.
HTMLDialogElement.returnValue
Is a DOMString that sets or returns the return value for the dialog.

Methods

Inherits methods from its parent, HTMLElement.

Name & Arguments Return Description
close() HTML5 void Closes the dialog. An optional DOMString may be passed as an argument, updating the returnValue of the the dialog.
show() HTML5 void Displays the dialog modelessly, i.e. still allowing interaction with content outside of the dialog.
showModal() HTML5 void Displays the dialog as a modal, over the top of any other dialogs that might be present. Interaction outside the dialog is blocked. User may press the "Escape" key to close the modal.

Examples

Example 1

<!-- Anchor point example -->
<dialog id="bronteDialog">
  <p>That was part of a poem by Emily Brontë!</p>
</dialog>

<blockquote>
  <p>"Then art thou glad to seek repose?<br>
  Art glad to leave the sea,<br>
  And <strong id="anchor">anchor</strong> all thy weary woes<br>
  In calm Eternity?"</p>
</blockquote>

<menu>
  <button id="showDialogButton">Show dialog</button>
</menu>

<script>
  (function() {
    var showDialogButton = document.getElementById('showDialogButton');

    // 'Show dialog' button opens dialog
    showDialogButton.addEventListener('click', function() {
      var bronteDialog = document.getElementById('bronteDialog');
      bronteDialog.show();
    });

  })();
</script>

Example 2

<!-- Simple pop-up dialog box, containing a form -->
<dialog id="favDialog">
  <form method="dialog">
    <section>
      <p><label for="favAnimal">Favorite animal:</label>
      <select id="favAnimal" name="favAnimal">
        <option></option>
        <option>Brine shrimp</option>
        <option>Red panda</option>
        <option>Spider monkey</option>
      </select></p>
    </section>
    <menu>
      <button id="cancel" type="reset">Cancel</button>
      <button type="submit">Confirm</button>
    </menu>
  </form>
</dialog>

<menu>
  <button id="updateDetails">Update details</button>
</menu>

<script>
  (function() {
    var updateButton = document.getElementById('updateDetails');
    var cancelButton = document.getElementById('cancel');

    // Update button opens a modal dialog
    updateButton.addEventListener('click', function() {
      document.getElementById('favDialog').showModal();
    });

    // Form cancel button closes the dialog box
    cancelButton.addEventListener('click', function() {
      document.getElementById('favDialog').close();
    });

  })();
</script>

Specifications

Specification Status Comment
WHATWG HTML Living Standard
The definition of '<dialog>' in that specification.
Living Standard
HTML5.1
The definition of '<dialog>' in that specification.
Recommendation

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 37 No support bug 840640 No support 24 No support
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support No support No support No support No support No support

See also

  • The HTML element implementing this interface: <dialog>.

© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement