Skip to content

JavaScript detect enter press on input | Example code

  • by

Use the keyup event to detect enter press on input in JavaScript. A keyCode 13 is the 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 an 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

Your email address will not be published. Required fields are marked *