Prevent popup window from getting close by escape key

By default an opened modal popup window gets close whenever an user presses ESC key on keyboard. In some cases we need to prevent popup window from getting close by escape key as a part of our requirements. Here I have written two methods to disable the function of ESC key using JavaScript and jQuery each.

 

Prevent modal popup from getting close by ESC key
Stop ESC key from closing  popup window using JavaScript:

The simplest method using JavaScript is 

Prevent popup window from getting close by escape key using jQuery: 

You can achieve the same using jQuery

This code above will disable the escape key from closing and this might interfere with other functions, and you probably could specify what element you want the event attached to.

Please note: You should place the code at the end of body to take precedence since most of the time there would be some code already attached in third party scripts which acts default behaviour (closing modal popup on ESC pressing) like the code below:

 

You Might Interested In

1 COMMENT

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.