how to press enter key programmatically in javascript code example
Example 1: jquery on enter key pressed
$(document).on('keypress',function(e) {
if(e.which == 13) {
alert('User pressed Enter!');
}
});
Example 2: javascript enter event listener
document.querySelector('#txtSearch').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
}
});
Example 3: javascript submit form when enter key pressed
document.getElementById("myInputID").addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
document.getElementById("myFormID").submit();
return false;
}
});
Example 4: javascript key pressed enter
$('.container').on('keydown', 'input', function(e) {
if (e.keyCode === 13) {
e.preventDefault();
e.stopImmediatePropagation();
}
});