/ Published in: jQuery
To check if an “enter†key is pressed inside a textbox, just bind the keypress() to the textbox.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<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>
URL: http://simply-tutorial.com/blog/2010/05/17/how-to-check-if-an-enter-key-is-pressed-with-jquery/