Hi so I am using these Modal / dialog boxes on my site. I am using code from W3-Schools which is jQuery if I'm not mistaken. They work fine and I was able to get them to close by clicking outside the box, but I am having trouble with having them close bu using the ESC key. The page that I have them on has 6 of them as an added element of difficulty. Any help would be greatly appreciated.
This is the code for the dialog box and the button to open
// Get the modal
var modal1 = document.getElementById('service1');
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
}
// These are the new code for using the ESC key (keycode = 27), but I have not had any luck
$("window").keydown(function(event) {
if (event.which == 27 & event.target == modal1) {
modal1.style.display = "none";
}
})
window.keydown = function(event) {
if (event.which == 27 & event.target == modal1) {
modal1.style.display = "none";
}
}
#service1 { display:none }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="button" onclick="document.getElementById('service1').style.display='block'"> Some Text
</button>
<div id="service1" class="w3-modal w3-margin-top">
<div class="w3-modal-content w3-card-4 w3-animate-top" style=" top:50px; width:61%; height:auto">
<header class="w3-container" style="height:auto;
background-color:#FE0565 ; color:#fff ;">
<span onclick="document.getElementById('service1').style.display='none'">
<i class="fa fa-close"></i></span>
<h2 style="text-align: center; font-size:34px; position:
relative;width:54%;margin-left:20%; top:0px;
text-decoration: underline"><b>Hard Drive</b></h2>
</header>
<div style="height:200px;">
<p></p>
</div>
<footer class="container" style="background-color:
#FE0565; color:#fff;">
<a href="/#">
<h3>For More Info Click Here</h3>
</a>
<span onclick="document.getElementById('service1').style.display='none'">
<i class="fa fa-close"></i></span>
</footer>
</div>
</div>