Close a Modal using the ESC Key? / Cerrar Modal usando ESC

 

 

 

$(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!»);
}
}
};

});
Continua leyendo «Close a Modal using the ESC Key? / Cerrar Modal usando ESC»

Entradas relacionadas

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *