Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch ESC in event in JQuery Dialog?

Is it possible to have a custom handler for ESC key on the JQuery Dialog?

like image 316
bozo Avatar asked Feb 13 '13 12:02

bozo


People also ask

What is escape keycode?

On computer keyboards, the Esc key Esc (named Escape key in the international standard series ISO/IEC 9995) is a key used to generate the escape character (which can be represented as ASCII code 27 in decimal, Unicode U+001B, or Ctrl + [ ).

What is dialog in jquery?

A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default. jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.


1 Answers

Yes, it's possible.

Set the closeOnEscape option to false and register your own keydown handler on the .ui-dialog element within the dialog's dialogcreate handler.

$(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

like image 191
Alnitak Avatar answered Oct 11 '22 11:10

Alnitak