JavaScript detect enter press on input | Example code

Use the keyup event to detect enter press on input in JavaScript. A keyCode 13 is key of ENTER.

How to detect enter press on input in JavaScript example

HTML example code, input filed to check the user presses enter key with the help of JavaScript.

<!DOCTYPE html>
<html>
<body>
  <input type="text" name="" id="myInputID">

  <script>
    var el = document.getElementById("myInputID");
    el.addEventListener("keyup", function(event) {
      if (event.key === "Enter") {
        alert(event.key  + " " + event.which);
      }
    });
  </script>
</body>
</html>

Output:

JavaScript detect enter press on input

Do comment if you know another way or have example on this JS key event topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.