files/en-us/web/api/htmldialogelement/open/index.md
{{ APIRef("HTML DOM") }}
The open property of the
{{domxref("HTMLDialogElement")}} interface is a boolean value reflecting the
open HTML attribute, indicating whether the {{htmlelement("dialog")}} is
available for interaction.
A boolean value representing the state of the open HTML attribute. A value of true means that the dialog is showing, while false means it's not showing.
[!WARNING] While the
openproperty is technically not read-only and can be set directly, doing so is strongly discouraged by the HTML specification, as it can break normal dialog interactions in unexpected ways. For example, thecloseevent won't fire when programmatically settingopentofalse, and subsequent calls to theclose()andrequestClose()methods will have no effect. Instead, it's better to use methods such asshow(),showModal(),close(), andrequestClose()to change the value of theopenattribute.
The following example shows a simple button that, when clicked, opens a {{htmlelement("dialog")}} containing a form via the showModal() method.
From there you can click the Cancel button to close the dialog (via the {{domxref("HTMLDialogElement.close()")}} method), or submit the form via the submit button.
The code logs the value of open when the dialog state changes.
<!-- Simple pop-up dialog box -->
<dialog id="dialog">
<form method="dialog">
<button type="submit">Close</button>
</form>
</dialog>
<button id="open">Open Dialog</button>
<pre id="log"></pre>
#log {
height: 170px;
overflow: scroll;
padding: 0.5rem;
border: 1px solid black;
}
const logElement = document.getElementById("log");
function log(text) {
logElement.innerText = `${logElement.innerText}${text}\n`;
logElement.scrollTop = logElement.scrollHeight;
}
const dialog = document.getElementById("dialog");
const openButton = document.getElementById("open");
function openCheck(dialog) {
log(dialog.open ? "Dialog: open" : "Dialog: closed");
}
openButton.addEventListener("click", () => {
dialog.showModal();
openCheck(dialog);
});
dialog.addEventListener("close", () => {
openCheck(dialog);
});
{{ EmbedLiveSample('Opening a dialog', '100%', '250px') }}
{{Specifications}}
{{Compat}}