The root of the problem is that the jQuery UI keydown event propagates through all the dialog boxes. The fix in the jQueryUI dialog source code would be to add event.stopPropagation() when the topmost dialog was successfully closed and mark event.isPropagationStopped() at the start of the same keydown event.
As a workaround, I did, thanks for Jazzer, the following.
Set the closeOnEscape dialog closeOnEscape to false
When the dialog is created, add:
//newDialog is dialog content, eg var newDialog = $('my dialog content>'); newDialog.keydown(function(event) { if (mydialogs.hasOpenDialogs() && event.keyCode && event.keyCode === $.ui.keyCode.ESCAPE) { $(newDialog).dialog("close"); event.preventDefault();
event.stopPropagation(); } });
when loading a document, do:
$(function(){ //allow for ESC to close only top dialog $(document).bind('keydown', function(event) { if (event.isPropagationStopped()) return true; if (mydialogs.hasOpenDialogs() && event.keyCode && event.keyCode === $.ui.keyCode.ESCAPE) { mydialogs.closeTopDialog(); event.preventDefault(); event.stopPropagation(); } }); });
The event in (2) occurs when the user presses ESC while entering text in the input field inside the dialog box. The event in (3) occurs when the user presses ESC somewhere else.
mydialogs here is a wrapper around the stack (array) of modal dialogs, where each new dialog box adds itself through .push() and in .close() removes itself using .pop() .
source share