Max Alexander Hanna
Max Alexander Hanna

Reputation: 3871

Check if HTML5 <dialog> is open

Is there a way to check if a <dialog> is open using javascript? For example using dialogId.isOpen() function ?

Upvotes: 7

Views: 7388

Answers (5)

Tushar Shahi
Tushar Shahi

Reputation: 20571

.open, but it is still experimental.

Upvotes: 1

Mike
Mike

Reputation: 24383

The HTMLDialogElement has the property open, which is a boolean whether the dialog is open or not.

I haven't tested, this, but I would assume you can do:

if (dialogId.open) {
  // do something
} else {
  // dialog is closed
}

Upvotes: 9

Atharva Sharma
Atharva Sharma

Reputation: 315

You can add an ID to the dialog and access it's open value using DOM like:

<dialog id="myDialog">This is an open dialog window</dialog>

<script>
function myFunction() {
  console.log(document.getElementById("myDialog").open);
}
</script>

Upvotes: 1

heokhe
heokhe

Reputation: 71

Use the .open property.

if (dialogElement.open) {
  // ...
}

More: https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/open

Upvotes: 3

E. Biagi
E. Biagi

Reputation: 482

I generally use a jQuery UI dialog.

$('#dialog').dialog('isOpen')

Upvotes: -1

Related Questions