How to catch ESC in event in JQuery Dialog?
Asked Answered
M

3

8

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

Mutualize answered 13/2, 2013 at 12:17 Comment(2)
one is already there called closeOnEscape: falseHalfbaked
@DipeshParmar that just turns off the default escape handler, it doesn't add a custom handler.Politics
P
14

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

Politics answered 13/2, 2013 at 12:19 Comment(0)
R
7

I use another way:

$(element).dialog({

    beforeClose: function(event) {
        if (event.keyCode === $.ui.keyCode.ESCAPE) {
            // ...
            return false;
        }
    }

});
Romonaromonda answered 27/6, 2013 at 16:37 Comment(0)
A
1
$(selector-for-dialog).keyup(function(e) {
    // ESC key
    if ( e.keyCode === 27 ) {
        // custom action
    }
});
Animality answered 13/2, 2013 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.