detect enter key javascript code example

Example 1: how to check if enter is pressed javascript

$('#usersearch').keydown(function(e){
    if(e.keyCode === 13){
            alert("Enter was pressed ");
     }  
});

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
    }
});