HTMLDialogElement

This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for the proper prefixes to use 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 spec 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.

Name Type Description
open Boolean Reflects the open HTML attribute, indicating that the dialog is available for interaction.
returnValue DOMString Gets/sets 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. An optional Element or MouseEvent may be passed as an argument, to specify an anchor point to which the dialog is fixed.
showModal() HTML5 void Displays the dialog for exclusive interaction, over the top of any other dialogs that might be present. An optional Element or MouseEvent may be passed as an argument, to specify an anchor point to which the dialog is fixed.

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, anchored at third line of quote
    showDialogButton.addEventListener('click', function() {
      var bronteDialog = document.getElementById('bronteDialog');
      var anchorPoint = document.getElementById('anchor');
      bronteDialog.show(anchorPoint);
    });

  })();
</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.
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 37 Not supported bug 840640 Not supported 24 Not supported
Anchor points Not supported Not supported Not supported Not supported Not supported
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support Not supported Not supported Not supported Not supported Not supported
Anchor points Not supported Not supported Not supported Not supported Not supported

See also

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

Document Tags and Contributors

 Contributors to this page: fscholz, MHasan, dhodder
 Last updated by: fscholz,