tags:

views:

3275

answers:

3

I would like to detect whether the user has pressed enter using Jquery.

How is this possible? does it require a plugiN?

EDIT: it looks like it is the keypress command. http://docs.jquery.com/Events/keypress I wanted to know if anyone knows if there are browser issues with that command - like are there any browser compatibility issues I should know about?

+1  A: 

link about this

http://www.rambeck.com/blog/2008/05/jquery-enter-tab

or

http://docs.jquery.com/Events/keypress

Haim Evgi
I would say only resorting to this article is a bit dangerous because instead of resorting to the jQuery keypress() event, they make their own browser checks, which kind of nullifies the whole idea of embracing a framework in the first place.
miek
+2  A: 

There's a keypress() event method. The Enter key's ascii number is 13 for all browsers.

BipedalShark
+7  A: 

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:

$(window).keypress(function(e) {
    if(e.keyCode == 13) {
        alert('You pressed enter!');
    }
});
Paolo Bergantino
thanks, that code works
chris