Actually, there was (is still) a bug in jQuery: http://bugs.jqueryui.com/ticket/4511.
The reason for this behavior (from the bug description comments): "The dialog itself binds keydown event to itself for closing the dialog on ESC; in addition, the dialog overlay binds a keydown event to the document, without filtering to close only the active dialog."
I cannot come up with an idea of an acceptable workaround. Is there anyone who has had to deal with it yet?
The root of the problem is that jQuery UI keydown event propagates through all dialogs. A fix in the original jQueryUI Dialog code would be to add
event.stopPropagation()
when topmost dialog was successfully closed and checkevent.isPropagationStopped()
at the beginning of the same keydown event.As a workaround I did, thanks for Jazzer, the following.
Set dialog option
closeOnEscape
tofalse
When dialog gets created, append:
when document is loaded do:
Event in (2) happens when user hits ESC while typing text in input box inside of the dialog. Event in (3) happens when user hits ESC somewhere else.
mydialogs
here is a wrapper around stack (array) of modal dialogs, where every new dialog adds itself via.push()
and in.close()
removes itself with.pop()
.Very simple - upon creating a modal dialog, run this:
If you create more then one modal dialog, hitting ESC will close the top one only. Then once you focus on the bottom dialog, hit ESC and it will close it as well.
Hope this helped!
P.S. jQuery UI developers should add an option when you want all dialogs close at once upon hitting ESC key or only the focused ones.
Easiest thing is I have added
event.stopPropagation();
in close function beforereturn self;
in jquery.ui.dialog.js file. And I am done with problem of closing dialog boxes one by one on escape keydown. Let me know if anyone find any better solution.EDITED: this need to add because while clicking on close button event object is undefined.