html event enter key pressed code example

Example 1: javascript submit form when enter key pressed

//submit form when 'Enter' key is pressed while in myInputID
document.getElementById("myInputID").addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
    	document.getElementById("myFormID").submit();
		return false;
    }
});

Example 2: javascript key pressed enter

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

Example 3: javascript detect enter press on input

var el = document.getElementById("myInputID");
el.addEventListener("keyup", function(event) {
    if (event.key === "Enter") {
        // Enter key was hit
    }
});

Tags:

Html Example