How do I detect an ESC key pressed in jQuery?

Asked 2 years ago, Updated 2 years ago, 31 views

Does the Esc key differ from browser to browser? It's 27 in Explo or Chrome and 0 in Firefox

$('body').keypress(function(e){
    alert(e.which);
    if(e.which == 27){
        // // Close my modal window
    }
});

javascript jquery

2022-09-22 22:07

1 Answers

$(document).keyup(function(e) {
     if (e.keyCode == 27) { // escape key maps to keycode `27`
        // // <DO YOUR WORK HERE>
    }
});

You can do it like this It's 27 regardless of browser.


2022-09-22 22:07

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.