how to trigger enter key in javascript code example

Example 1: javascript enter event listener

document.querySelector('#txtSearch').addEventListener('keypress', function (e) {
    if (e.key === 'Enter') {
      // code for enter
    }
});

Example 2: hit enter button lick in js

<form>
    <input id="myInput" placeholder="Some text.." value="">
    <input type="submit" id="myBtn" value="Submit">
</form>

<script>
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("myBtn").click();
    }
});
</script>

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