js key enter code code example

Example 1: javascript key pressed enter

$('.container').on('keydown', 'input', function(e) {
  if (e.keyCode === 13) {
    e.preventDefault();
  	e.stopImmediatePropagation();
    //Do your stuff...
  }
});

Example 2: input enter button js

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

  
input.addEventListener("keyup", (event) => {
    if (event.key === "Enter") {
        // Cancel the default action, if needed
        event.preventDefault();
        // Trigger the button element with a click
        document.getElementById("myBtn").click();
    }
})

Tags:

Html Example