HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptCritical

How can I detect pressing Enter on the keyboard using jQuery?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
enterkeyboardhowdetectjquerytheusingcanpressing

Problem

I would like to detect whether the user has pressed Enter using jQuery.

How is this possible? Does it require a plugin?

It looks like I need to use the keypress() method.

Are there browser issues with that command - like are there any browser compatibility issues I should know about?

Solution

The whole point of jQuery is that you don't have to worry about browser differences. I am pretty sure you can safely go with enter being 13 in all browsers. So with that in mind, you can do this:

$(document).on('keypress',function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

Code Snippets

$(document).on('keypress',function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});

Context

Stack Overflow Q#979662, score: 1511

Revisions (0)

No revisions yet.