Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example of Keydown Event</title> <style> .highlight { background-color: yellow; } </style> <script> document.addEventListener('DOMContentLoaded', function () { const inputField = document.getElementById('inputField'); inputField.addEventListener('keydown', function(event) { console.log('Key down:', event.key); if (event.key === "Enter") { this.classList.add('highlight'); event.preventDefault(); // Prevents the default action of the enter key } }); }); </script> </head> <body> <input type="text" id="inputField" placeholder="Press 'Enter' to highlight"> </body> </html>