How to catch ESC in case in jQuery Dialog?

Is it possible to have a special handler for the ESC key in the jQuery dialog?

+8
jquery jquery-dialog
source share
3 answers

Yes it is possible.

Set closeOnEscape to false and register your own keydown handler with the .ui-dialog element in the dialogcreate dialog box.

 $(element).dialog({ create: function() { $(this).closest('.ui-dialog').on('keydown', function(ev) { if (ev.keyCode === $.ui.keyCode.ESCAPE) { ... } }); ... }, closeOnEscape: false, ... }); 

See http://jsfiddle.net/alnitak/EbnZr

+12
source share

I use a different way:

 $(element).dialog({ beforeClose: function(event) { if (event.keyCode === $.ui.keyCode.ESCAPE) { // ... return false; } } }); 
+7
source share
 $(selector-for-dialog).keyup(function(e) { // ESC key if ( e.keyCode === 27 ) { // custom action } }); 
+1
source share

All Articles