Skip to content
Advertisement

jQuery UI – Close Dialog When Clicked Outside

I have a jQuery UI Dialog that gets displayed when specific elements are clicked. I would like to close the dialog if a click occurs anywhere other than on those triggering elements or the dialog itself.

Here’s the code for opening the dialog:

JavaScript

If I uncomment the last part, the dialog never opens. I assume it’s because the same click that opens the dialog is closing it again.


Final Working Code
Note: This is using the jQuery outside events plugin

JavaScript

Advertisement

Answer

Check out the jQuery Outside Events plugin

Lets you do:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement