Return to Snippet

Revision: 68378
at January 14, 2015 14:11 by luhonghai


Initial Code
<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>

Initial URL
http://simply-tutorial.com/blog/2010/05/17/how-to-check-if-an-enter-key-is-pressed-with-jquery/

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

Initial Title
Check if an Enter Key Is Pressed With jQuery

Initial Tags
javascript, jquery

Initial Language
jQuery