jQuery: Detect enter key

June 6, 2010

Sometimes it is nice to be able to detect when the user has pressed the enter. This is great for super custom forms and Javascript games. Or maybe a totally custom built app that is waiting for the user to hit enter for some reason. But how to detect the enter key? Luckily jQuery once again comes in handy. Check out the following bit of code to grab the enter key.

$('#keyed_element').keyup(function(e) {
    if(e.keyCode == 13) {
        // Put your key handler code here
    }
});
Leave a Reply

Your email address will not be published. Required fields are marked *