Search code examples
jqueryasp.net-mvckeypress

How to send Escape key using JQuery


When user clicks some button on my MVC view, I want to simulate the event that gets executed on Escape key. How can I achieve it using JQuery?


Solution

  • function pressEsc() {
        $('body').trigger({
            type: 'keyup',
            which: 27 // Escape key
        });
    }
    
    $(function () {
        $('body').on('keyup', function (e) {
            alert(e.which + ' key was pressed');
        });
    
        // Press the escape key
        pressEsc();
    });
    

    Demo : http://jsfiddle.net/T4H89/