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?
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?
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();
}
});
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
}
})
<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
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!");
}
}
};
});
try this code :
<script type="text/javascript">
$(document).keyup(function(event){
if(event.which=='27'){
$('.cd-popup').removeClass('is-visible');
}
});
</script>
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
.