Detect if a jQuery UI dialog box is open

JqueryJquery UiJquery Ui-Dialog

Jquery Problem Overview


I am using a jQuery UI dialog. If it is open, I want to do one thing. If it is closed, I want to do another.

My question is, how do I detect if a jQuery UI dialog box is open or not?

Jquery Solutions


Solution 1 - Jquery

If you read the docs.

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

This method returns a Boolean (true or false), not a jQuery object.

Solution 2 - Jquery

Actually, you have to explicitly compare it to true. If the dialog doesn't exist yet, it will not return false (as you would expect), it will return a DOM object.

if ($('#mydialog').dialog('isOpen') === true) {
    // true
} else {
    // false
}

Solution 3 - Jquery

If you want to check if the dialog's open on a particular element you can do this:

if ($('#elem').closest('.ui-dialog').is(':visible')) { 
  // do something
}

Or if you just want to check if the element itself is visible you can do:

if ($('#elem').is(':visible')) { 
  // do something
}

Or...

if ($('#elem:visible').length) { 
  // do something
}

Solution 4 - Jquery

jQuery dialog has an isOpen property that can be used to check if a jQuery dialog is open or not.

You can see example at this link: http://www.codegateway.com/2012/02/detect-if-jquery-dialog-box-is-open.html

Solution 5 - Jquery

Nick Craver's comment is the simplest to avoid the error that occurs if the dialog has not yet been defined:

if ($('#elem').is(':visible')) { 
  // do something
}

You should set visibility in your CSS first though, using simply:

#elem { display: none; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser208662View Question on Stackoverflow
Solution 1 - JqueryByron WhitlockView Answer on Stackoverflow
Solution 2 - JquerymarcovtwoutView Answer on Stackoverflow
Solution 3 - JqueryNick CraverView Answer on Stackoverflow
Solution 4 - JqueryAvinashView Answer on Stackoverflow
Solution 5 - Jqueryuser2452922View Answer on Stackoverflow