I'm having an issue where I want to load an iframe when a button is clicked by appending a modal and the iframe to the end of the body tag with a function like this:
function openDonateModal(){
if( document.querySelector('#donate-modal') != null){
document.querySelector('#donate-modal').style.display = 'block';
} else{
document.querySelector('body').innerHTML +=`
<div id="donate-modal" onclick="this.style.display='none'">
<iframe loading="lazy" src="someOtherSite.com">Loading...</iframe>
</div>
`
}
}
document.querySelector('.donate-button').addEventListener('click', openDonateModal);
And this works one single time for each page refresh, after that it stops working, and I've found that the eventListener is not even attached to the button any more.
I thought the problem was isolated to the listener that I'm adding here, but it seems to "un-attach" all the listeners on the page, e.g. drop down menus don't work anymore. I've confirmed it happens in both Firefox and Chrome.