How to close all open JQuery dialogs

Before upgrading to the newest library for jQuery I was using this code to close any open dialogs. Now this code just throws an error.

  $(".dialogs:ui-dialog").each(function () {

            if ($(this).data('dialog').isOpen()) {
               $(this).dialog('close');
          };
 });

What I need to accomplish is when a jQuery dialog is open and the user clicks on a menu item to open another dialog I need to make sure that all other dialogs are closed before the new one is opened.

What is the new code to use to accomplish this task?

Upvotes: 2

Views: 1011

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68400

You could simply use

$( ".dialogs" ).dialog( "close" );

If you check the dialog source code you'll see that closing an already closed dialog have no effect so you can safely use it

close: function( event ) {
    var that = this;

    if ( !this._isOpen || this._trigger( "beforeClose", event ) === false ) {
        return;
    }
    ...
}

If you want to keep your logic similar to what you have for any reason, you could use this

$(".dialogs").each(function () {
    var $dialog = $(this);
    if ($dialog.dialog('isOpen')) {
        $dialog.dialog('close');
    };
});

Upvotes: 3

Related Questions