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