Main Tutorials

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 Author

author image
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

Subscribe
Notify of
19 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Fernando
9 years ago

Thanks. Very well explained

Nuhu Ibrahim
4 years ago

Thanks very much

thanks
10 years ago

Thanks.

plantar warts
10 years ago

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

Peggy
10 years ago

Can you envision that?

buswale
10 years ago

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

Rizaldi
10 years ago

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

Surficle
10 years ago

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
10 years ago

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

mark
10 years ago

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

anonymous
11 years ago

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
11 years ago

does not work on localhost using wampserver ……….

Teh Dude
11 years ago
Reply to  ramin

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

João Paulo
9 years ago

what is

event.stopPropagation();

?

Ann taylor coupons
10 years ago

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
11 years ago

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
11 years ago

i like your website it help me all the time 🙂

sudarshan balajirao
11 years ago

very helpful website

Rodney
11 years ago

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)