Are you trying to submit a form?
Listen to the submit event instead.
This will handle click and enter.
If you must use enter key...
document.querySelector('#txtSearch').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
// code for enter
}
});