Back to all resources
Event Listener
Keydown

Click Something When Pressing ESC Key

By Corey Moen
This JavaScript code snippet adds an event listener to the document body that listens for 'keydown' events. When a key is pressed, the event listener checks if the key is the 'Escape' key. If it is, the code selects the first HTML element with the class name 'class' and triggers a click event on it. The comment at the top of the code suggests updating the '.class' selector to match the class of the element you want to click when the 'Escape' key is pressed.