Key Press to Trigger a Click (or anything really!)

For anyone looking to use keys to do trigger a click (or anything really!).

In this example, when a user presses the esc button (keycode 27), it triggers a click on a button with the class name close-popup-button.

 <script>
      
      	$(document).on('keyup', function(event) {
      	else if (event.which === 27) {
         	$('.close-popup-button').click();
       		}
        });
      
 </script>

All key codes can be found here:

9 Likes