views:

49

answers:

2

I know I can use the following to close the dialog box by clicking outside:

$('.ui-widget-overlay').click(function() { $("#dialog").dialog("close"); });

But how do I change this so it works for every dialog box, ie I want to say close any dialog box as we have multiple on a page and would be easier to have one line of code?

A: 

Maybe this is overkill, but try

$('.ui-widget-overlay').live('click',
    function() {
        $(".ui-dialog").dialog("close");
    }
);

You only need to run this code once on your page, the live method should make it work any time you open a dialog.

EDIT: If this isn't working, it might be .dialog's fault. Try

$('.ui-widget-overlay').live('click',
    function() {
        $(".ui-dialog").each(
            function() {
               $(this).dialog("close");
            }
        );
    }
);
MvanGeest
Thanks I tried this however it doesnt seem to work. via Chrome Dev Tools, I can see the dialog box does have a class of ui-dialog so unsure why this isnt working.It works when I specify the ID.
Igor K
OK, so could you try the second example?
MvanGeest
Thanks MvanGeest, unfortunately that doesn't work either.
Igor K
+1  A: 

you can give each dialog a class

and then select it and run on each and cose it even if its not open it will work:

$('.ui-widget-overlay').click(function() { $(".dialogs").each(function()
 {$(this).dialog("close");}) });  
guy schaller
The UI code already gives every dialog the class `ui-dialog`, as described on http://docs.jquery.com/UI/Dialog.
MvanGeest