22

I have made a modal box popup functionality and I want to close this modal popup up box when someone hits the escape key, in all browsers. I have used the file modal-window.min.js for these popups.

How can I close these in response to this key?

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
user1503100
  • 229
  • 1
  • 2
  • 3
  • 1
    refer to http://stackoverflow.com/questions/1160008/which-keycode-for-escape-key-with-jquery in addition i guest to use tinybox which doesn't have to do anything with jquery – MySqlError Oct 10 '12 at 09:54

7 Answers7

36

With the keydown function:

$(document).keydown(function(event) { 
  if (event.keyCode == 27) { 
    $('#modal_id').hide();
  }
});

Note: Prefer using keydown for the Escape key as in some browsers the keypress event is only fired if the key outputs a character

Ramon Balthazar
  • 3,907
  • 3
  • 25
  • 34
19
$(document).keypress(function(e) { 
    if (e.keyCode === 27) { 
        $("#popdiv").fadeOut(500);
        //or
        window.close();
    } 
});
Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
StaticVariable
  • 5,253
  • 4
  • 23
  • 45
16

For the people landing on this looking for a non-jQuery solution, here it is:

document.addEventListener('keydown', (event) => {
  if (event.key === 'Escape') {
    // close modal here
  }
})
Alec Rust
  • 10,532
  • 12
  • 48
  • 63
2
<script type="text/javascript">
 jQuery(document).keypress(function(e) {
  if (e.keyCode === 27) {
   jQuery("#myModal").modal('toggle');
                 OR
   jQuery("#myModal").modal('hide');
  }
 });
</script>

Taken from: http://chandreshrana.blogspot.in/2016/05/how-to-close-model-popup-on-escape-key.html

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
Chandresh
  • 361
  • 2
  • 7
2

If you have more than one modal, you can use script below. I had to open so many modal in one page, thats why i wrote this script. This script closes modals one by one according to opening order with escape key. And also you don't need to define any modal name in script hence add once use everywhere.

var modals=[]; // array to store modal id

$(document).ready(function(){

$('.modal').modal({show: false, keyboard: false}); // disable keyboard because it conflicts below

$('.modal').on('show.bs.modal', function (event) {
   //add modal id to array
   modals.push(event.target.id);
});


document.onkeydown = function(evt) {
    evt = evt || window.event;
    var isEscape = false;
    if ("key" in evt) {
        isEscape = (evt.key === "Escape" || evt.key === "Esc");
    } else {
        isEscape = (evt.keyCode === 27);
    }
    if (isEscape) {

        if(modals.length>0){
            //get last modal id by using pop(). 
            //This function also removes last item in array.
            var id = modals.pop();
            if($('#'+id).is(':visible')){ // if modal is not closed by click or close button
                $('#'+id).modal('toggle');
            }
        }else{
            alert("Could not find any modals!");
        }
    }
};

});
Fatih
  • 21
  • 1
0

try this code :

<script type="text/javascript">
   $(document).keyup(function(event){
      if(event.which=='27'){
          $('.cd-popup').removeClass('is-visible');
      }
   });
</script>
Mehul Velani
  • 691
  • 9
  • 13
-1

Using AngularJs: In case you have multiple modals,

assign a value to each modal, when you open the modal set it to true when you close it set it to false.

//in your .js script

    document.onkeydown = function (event) {
    event = event || window.event;
    if (event.keyCode === 27) {
        if (usersModal)
            $scope.hideUsersModal();
        else if (groupsModal)
            $scope.hideGroupsModal();
    }

Using Angular, event.target property indicates where you've set the ng-controller and event.currentTarget is the #document.

Jamie Counsell
  • 7,730
  • 6
  • 46
  • 81
Amir Hossein Abdollahi
  • 1,253
  • 2
  • 9
  • 14