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>
Thanks. Very well explained
Thanks very much
Thanks.
There are variousmoles and wart removalmethods that can be employed to safely and practically painlessly remove these undesired skin indentations.
Can you envision that?
Nice Tutorial.Thank You For Sharing Great Information For Us.
Waw!!!! is GREAT tutorial. Thank you for sharing.
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;
});
???????, ????! ??? ??? ??, ??? ????.
There is certainly a lot to learn about this
issue. I really like all of the points you’ve made.
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 @@
does not work on localhost using wampserver ……….
Its browser dependent and has nothing to do with the server its running on. So you just have an error guy.
what is
event.stopPropagation();
?
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!
Why store the key code in a variable if you can just:
Personally I’d prefer not to use jQuery and just:
i like your website it help me all the time 🙂
very helpful website
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:
it would be more correct (and faster) if the keycode was treated as the number it is: