How TO - Trigger Button Click on Enter


Trigger a button click on keyboard "enter" with JavaScript.


Trigger a Button Click on Enter

Press the "Enter" key inside the input field to trigger the button:

Example

// Get the input field
var input = document.getElementById("myInput");

// Execute a function when the user presses a key on the keyboard
input.addEventListener("keypress", function(event) {
  // If the user presses the "Enter" key on the keyboard
  if (event.key === "Enter") {
    // Cancel the default action, if needed
    event.preventDefault();
    // Trigger the button element with a click
    document.getElementById("myBtn").click();
  }
});
Try it Yourself »

Tip: Learn more about the KeyboardEvent key Property in our JavaScript Reference.


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.