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>

19 comments on “How to check if an enter key is pressed with jQuery

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

    Reply
  2. 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;
    });

    Reply
  3. 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!

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

    Reply
  5. 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;
    Reply
  6. 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 @@

    Reply
  7. 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)
    Reply
  8. does not work on localhost using wampserver ……….

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

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *