Enter key press event in JavaScript


Enter key press event in JavaScript : Sometimes we need to trigger some action based upon the key events such as key pess, key down etc and some specific case like trigger action on enter press. For this type of events we use key code to identify the key press, using key code you can perform the action as per your requirement. Here in this tutorial we are going to explain how you can bind some event action when enter button is pressed. You can also use the try it functionality to see the working demo.


Enter key press event in JavaScript

The key code for enter button is 13. You can use this to detect the enter button click. Here is example which shows alert when the enter button is clicked.

Enter key press event in JavaScript Example:

<html>
<head>
<title>Javascript Demo</title>
<script type='text/javascript'>
function checkEnterClick(e){
   if(e.keyCode == 13){
   alert("You Have Pressed Enter Key.");
   }
}
</script>
</head>
<body>

Press Enter Button in text box
<input type='text' onkeypress='checkEnterClick(event)' name='test'>
</body>
</html>

Try it »

In the above example we have created a function which is called on keypress event. As soon as the enter key is pressed in the text box the function will be called and e.keyCode will give the 13 which will generate a alert as above.

Enter Key Press Event : Output Of Example

if you run the above example it will produce output something like this-

Enter key press event in JavaScript


Advertisements

Add Comment

📖 Read More