How to detect escape key press with pure JS or jQuery?

Cover Image for How to detect escape key press with pure JS or jQuery?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Detect Escape Key Press with Pure JS or jQuery?

Are you struggling to detect the escape key press in different browsers like IE, Firefox, and Chrome? We have got you covered! In this blog post, we will address this common issue and provide you with easy solutions using both pure JavaScript and jQuery. So, let's dive right in!

The first thing you need to understand is that different browsers have different ways of handling key events. This is why the code you mentioned may not work consistently across all browsers. But don't worry, we have some handy solutions for you.

Pure JavaScript Solution

document.addEventListener('keydown', function(event) {
  if (event.key === "Escape" || event.key === "Esc") {
    // Close your modal window or perform any desired action
  }
});

In this solution, we use the keydown event to detect when a key is pressed. We then check if the key that was pressed is either "Escape" or "Esc". If it matches, you can implement your desired action, such as closing a modal window.

jQuery Solution

$(document).on('keydown', function(e) {
  if (e.key === "Escape" || e.key === "Esc") {
    // Close your modal window or perform any desired action
  }
});

For those who prefer using jQuery, we have an alternative solution. We use the keydown event and check for the same key values as in the pure JavaScript solution. Again, when a match is found, you can close your modal window or perform any other necessary action.

Cross-Browser Solution

If you want a solution that works consistently across all major browsers, including IE, Firefox, and Chrome, you can combine both approaches:

function onEscapeKey(event) {
  if (event.key === "Escape" || event.key === "Esc" || event.keyCode === 27) {
    // Close your modal window or perform any desired action
  }
}

document.addEventListener('keydown', onEscapeKey);
$(document).on('keydown', onEscapeKey);

By combining both pure JavaScript and jQuery solutions, you can ensure that your code handles the escape key press reliably across different browsers.

Conclusion

Detecting the escape key press may seem like a simple task, but it can become tricky when dealing with different browsers. However, with the solutions provided in this blog post, you can easily detect the escape key press using either pure JavaScript or jQuery. Remember to choose the solution that best fits your project and enjoy consistent cross-browser functionality.

If you have any questions or other key-related issues you'd like assistance with, let us know in the comments below. Happy coding! 💻🚀

Note: This post was inspired by a question on the Stack Overflow community. You can find the original question here.


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello