How can I tell if a jquery ui dialog query has been initialized?
Asked Answered
A

3

12

I have the following code to detect if a jquery ui dialog is open:

if ($("#dialog-myDialog").dialog("isOpen")) {
      return;
}

which works fine but I found a situation where this code get called prior to the dialog being initialized in the first place and this if statement seems to just blow up in this case.

What is the best way to check if a jquery ui dialog has been initialized so I can properly handle this situation.

Applicative answered 9/4, 2015 at 2:27 Comment(0)
F
30

Test whether the element has the ui-dialog-content class:

if ($("#dialog-myDialog").hasClass("ui-dialog-content") &&
    $("#dialog-myDialog").dialog("isOpen")) {
    return;
}
Forefend answered 9/4, 2015 at 2:33 Comment(0)
A
3

If you use a solution which relies on the presence of a css class added by a component that is out of your control, then you run the risk of this not working if a new version of the component changes the way it manages classes.

A more reliable solution would be to add your own existence indicator at dialog initialization:

$("#popup").attr("_dialogInitialized", "yes").dialog( { ... } )

Then check for your indicator when you need to:

if ($("#popup[_dialogInitialized]").length == 1) {
    // dialog has been previously initialized
} else {
    // dialog has been not yet been initialized
}
Anthozoan answered 17/6, 2016 at 8:28 Comment(0)
F
-1

Add a class when initilizing:

$("selector").addClass("initialized").dialog( { ... } );

Then check for the class when needed:

if ($("selector").hasClass("initialized")) { ... }
Firing answered 26/2, 2017 at 14:41 Comment(1)
Sometimes it is incorrect. dialog() will call resize event handler, and during this call dialog is still not initialized, but class is already present.Moleskins

© 2022 - 2024 — McMap. All rights reserved.