I have been trying to make a div ("aside") show when I click on a button, then hide when I click out of the div, but my code simply hides it instantly when I click the button.
Here is the HTML:
<aside id="modal_champion" style="display: none;">
<div id="modal_champion_wrapper">
<div id="champion_loading_screen">
<img loading="lazy" id="champion_loading_screen_image">
<figcaption id="champion_loading_screen_infos">
<span id="champion_loading_screen_name"></span>
<span id="champion_loading_screen_title"></span>
</figcaption>
</div>
<button id="modal_close">×</button>
</div>
</aside>
And the Javascript:
async function open_modal() {
/* Some code above...*/
// Close modal by clicking out of it
window.addEventListener('click', function (e) {
if (!document.getElementById('modal_champion_wrapper').contains(e.target)) {
console.log('HIDE!')
return document.getElementById('modal_champion').style.display = 'none';
}
});
}
To see it without downloading the project, you can see the website on GitHub or on this environment.