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?
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..
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.
You have two options to close this modal: Click on the "x" or click anywhere outside of the 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.
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 thekeypress
event is only fired if the key outputs a character
$(document).keypress(function(e) {
if (e.keyCode === 27) {
$("#popdiv").fadeOut(500);
//or
window.close();
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With