Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I close a modal popup on hitting escape?

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?

like image 324
user1503100 Avatar asked Oct 10 '12 09:10

user1503100


People also ask

How do I close pop ups on escape?

There is no default option to close the reactive popup by pressing the esc key. You need to create event for the popup like Onkeypress or Onkeyup to trigger event. After that it will be possible to close popup by esc key..

How do you close modal with ESC key in react?

responsive: full width on small devices, fixed width on bigger ones; accepting every kind of content: html, images or others React components; three way to close the modal: using the ESC key, clicking outside the window, clicking on a dedicated close “X” button.

How do you close a modal by clicking outside?

You have two options to close this modal: Click on the "x" or click anywhere outside of the modal!

How do you force close a modal?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.


2 Answers

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

like image 166
Ramon Balthazar Avatar answered Sep 30 '22 23:09

Ramon Balthazar


$(document).keypress(function(e) { 
    if (e.keyCode === 27) { 
        $("#popdiv").fadeOut(500);
        //or
        window.close();
    } 
});
like image 26
StaticVariable Avatar answered Sep 30 '22 22:09

StaticVariable