I am attempting to close a modal when the user clicks outside of it, using JQuery instead of plain JavaScript like in the W3C website example. The $modal variable contains the jQuery object representing the HTML div used for the modal.
I came across this code on stackoverflow, but it did not work:
let $window = $('window', opener.document);
window.parent.$window.on('click', function(event){
if (event.target == $modal){
$modal.css({display:'none'});
}
});
This is what I tried myself:
let $window = $('window');
window.on('click', function(event){
if(event.target == $modal){
$modal.css({display:'none'});
}
}
Trying to mix Plain JavaScript with JQuery:
let $window = $('window');
window.onclick = function(event){
if(event.target == $modal){
$modal.css({display:'none'});
}
}
Is there something I am overlooking?