I need to execute a JavaScript function when the enter key is pressed, i tried KeyCode
but I found out it's deprecated,I can't do it in JQuery because i'm executing it at the end of the code, so it would be too late because someone could press enter before JQuery execution. Thanks in advance
Asked
Active
Viewed 340 times
-1

123Lory321
- 1
- 2
-
2Does this answer your question? [Enter key press event in JavaScript](https://stackoverflow.com/questions/905222/enter-key-press-event-in-javascript) – Marcelo The Mage Coder Feb 03 '20 at 14:43
-
The following code will work fine. onkeydown="javascript:if ((event.which && event.which == 13)) { alert('Enter Key pressed');}" – Melvin Feb 03 '20 at 15:09
1 Answers
0
This is the simplest way:
document.onkeypress = function (e) {
if (e.keyCode == 13) alert("Enter");
};

Ahmed Tag Amer
- 1,381
- 1
- 8
- 21