Eventlistener Enter Key
Is there an addEventListener for the Enter key? I have document.querySelector('#txtSearch').addEventListener('click', search_merchants); I know this is intended for
Solution 1:
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
}
});
Solution 2:
Here is a version of the currently accepted answer (from @Trevor) with key instead of keyCode:
document.querySelector('#txtSearch').addEventListener('keypress', function (e) {
if (e.key === 'Enter') {
// code for enter
}
});
Solution 3:
You could listen to the 'keydown' event and then check for an enter key.
Your handler would be like:
function (e) {
if (13 == e.keyCode) {
... do whatever ...
}
}


Post a Comment for "Eventlistener Enter Key"