I've tried document.addEventListener("keydown", function() {})
, but dont know how to listen for when a user presses enter
Asked
Active
Viewed 45 times
-3

AK CRAWFORD
- 1
- 2
-
[Keyboard event: code](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code). – Andy Jul 13 '22 at 09:28
-
1You can read this in the documentation of MDN: https://developer.mozilla.org/en-US/docs/Web/API/Element/keydown_event – Kokodoko Jul 13 '22 at 09:28
-
But have you tried putting your title in a web search engine? – gre_gor Jul 13 '22 at 10:25
2 Answers
0
Check the key
property of event
(keyboard event).
document.addEventListener('keydown', function(event) {
if (event.key === 'Enter') {
console.log(event.key);
}
});

User456
- 1,216
- 4
- 15
-2
Have you tried this?
document.addEventListener("keyup", function(event) {
if (event.code === 'Enter') {
alert('Enter is pressed!');
}
});

Ben
- 116
- 3
- 14