RSS

Handling keypress event in jQuery

28 Aug

To determine which key is pressed by the user, simply check jQuery event.which property for the keycode. The example below binds textarea elements with keypress event and cancel the default Enter key function:

$("textarea").keypress(function(e) {
    // If the key pressed is Enter
    if ( e.which === 13 ) {
        // Disable line break
        e.preventDefault();

        // Do things

    }
});

Here are 2 useful links to look up javascript keycode:
Javascript Char Codes (Key Codes) – Cambia Research
JavaScript Event KeyCode Test Page

Advertisements
 
Leave a comment

Posted by on August 28, 2012 in Javascript, jQuery

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: