How to check if an enter key is pressed with jQuery

The “enter” key is represent by code “13”, check this ASCII charts.

To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox.


$('#textbox').keypress(function(event){
	
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in textbox');	
	}

});

To check if an enter key is pressed on page, bind the keypress() to the jQuery $(document).


$(document).keypress(function(event){
	
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in somewhere');	
	}
	
});

P.S In Firefox, you have to use event.which to get the keycode; while IE support both event.keyCode and event.which.

Try it yourself


<html>
<head>

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

</head>
<body>
  <h1>Check if "enter" is pressed with jQuery</h1>

<label>TextBox : </label>
<input id="textbox" type="text" size="50" />

<script type="text/javascript">

$('#textbox').keypress(function(event){
	
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in textbox');	
	}
	event.stopPropagation();
});

$(document).keypress(function(event){
	
	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in somewhere');	
	}
	
});

</script>
</body>
</html>

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
18 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
19 Comment authors
Nuhu IbrahimJoão PauloFernandothanksplantar warts Recent comment authors
newest oldest most voted
Peggy
Guest
Peggy

Can you envision that?

João Paulo
Guest
João Paulo

what is

event.stopPropagation();

?

Fernando
Guest
Fernando

Thanks. Very well explained

Nuhu Ibrahim
Guest
Nuhu Ibrahim

Thanks very much

thanks
Guest
thanks

Thanks.

plantar warts
Guest
plantar warts

There are variousmoles and wart removalmethods that can be employed to safely and practically painlessly remove these undesired skin indentations.

buswale
Guest
buswale

Nice Tutorial.Thank You For Sharing Great Information For Us.

Rizaldi
Guest
Rizaldi

Waw!!!! is GREAT tutorial. Thank you for sharing.

Surficle
Guest
Surficle

hello, mkyong
nice and simple tutorial..
You can provide more in detail key code for the key event to the reader can understand, and also the combination of the key like..

$(document).keypress(function(event) {
if ((event.which == 115 && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {
event.preventDefault();
// do stuff
return false;
}
return true;
});

skrabus
Guest
skrabus

???????, ????! ??? ??? ??, ??? ????.

mark
Guest
mark

There is certainly a lot to learn about this
issue. I really like all of the points you’ve made.

anonymous
Guest
anonymous

Help me! I use this keypress to check captcha. For test, i type a character then alert() it, but i can not get the character i type. For example, i type “a”, alert() no thing, i continous type “b” (now i have “ab”) but alert() show “a” only @@

ramin
Guest
ramin

does not work on localhost using wampserver ……….

Teh Dude
Guest
Teh Dude

Its browser dependent and has nothing to do with the server its running on. So you just have an error guy.

Ann taylor coupons
Guest
Ann taylor coupons

I used to be recommended this web site by means
of my cousin. I’m no longer positive whether or not this post is written by way of him as nobody else realize such detailed about my trouble. You’re wonderful!
Thanks!

yendoR
Guest
yendoR

Why store the key code in a variable if you can just:

function(event){if(event.keyCode ? event.keyCode : event.which == 13)enterKeyPressed();}

Personally I’d prefer not to use jQuery and just:

element.onkeypress = func;
aymen
Guest
aymen

i like your website it help me all the time :)

sudarshan balajirao
Guest
sudarshan balajirao

very helpful website

Rodney
Guest
Rodney

Thanks for making this page, I needed a refresher….

One thing I have a gripe with though – treating the keycode as a string, as in:

if(keycode == '13')

it would be more correct (and faster) if the keycode was treated as the number it is:

if(keycode === 13)