call function when user hits enter in textinput html code example

Example: call function when user hits enter in textinput html

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

// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup", function(event) {
  if (event.code === "Enter") {
    // Cancel the default action, if needed
    event.preventDefault();
    // Trigger the button element with a click
    document.getElementById("myBtn").click();
    // ...or call a function
    myFunction();
  }
});

Tags:

Misc Example