javascripthtmlmodal-dialog

How to close modal by pressing esc


I am new to Javascript and I'd like to close this modal by pressing esc key, besides clicking on span. How to write it?

var modal = document.getElementById('myModal');
                        var img = document.getElementById('myImg');
                        var img2 = document.getElementById('myImg2');
                        var img3 = document.getElementById('myImg3');
                        var img4 = document.getElementById('myImg4');
                        var modalImg1 = document.getElementById("img01");
                        var captionText = document.getElementById("caption");

                            img.onclick = function()
                                {
                                    modal.style.display = "block";
                                    modalImg1.src = this.src;
                                    captionText.innerHTML = this.alt;
                                }
                            img2.onclick = function()
                                {
                                    modal.style.display = "block";
                                    modalImg1.src = this.src;
                                    captionText.innerHTML = this.alt;
                                }   
                            img3.onclick = function()
                                {
                                    modal.style.display = "block";
                                    modalImg1.src = this.src;
                                    captionText.innerHTML = this.alt;
                                }   
                            img4.onclick = function()
                                {
                                    modal.style.display = "block";
                                    modalImg1.src = this.src;
                                    captionText.innerHTML = this.alt;
                                }   

                            var span = document.getElementsByClassName("close")[0];

                            span.onclick = function() 
                            { 
                                modal.style.display = "none"; 
                            }

I'm considering to add after that span:

window.onkeypress = function (event) 
                            {
                                if (event.keyCode == 27 && modal.style.display='block') 
                                    {
                                        modal.style.display='none';
                                    }
                            }

but don't honestly know what's wrong there

Thanks for any help.


Solution

  • Try with onkeyup instead of onkeypress:

    window.onkeyup = function (event) {
        if (event.keyCode == '27' && modal.style.display=='block') {
            modal.style.display='none';
        }
    }
    

    Why?

    Using onkeypress you'll get different key codes for different browsers. Check these threads on SO: Which keycode for escape key with jQuery And: How to detect escape key press with pure JS or jQuery?