Sorry for the title i don't know how to explain it differently. I created a popup window, there is an "x" close button on right top, advanced and close button on mid bottom. After clicking and replacing text in HTML those newly created buttons just doesn't work.
let mda = document.getElementById("popup");
document.addEventListener("DOMContentLoaded", () => { //show the pop up after loading the stie
mda.style.display = "block";
});
const adv = document.querySelector(".adv"); //Advanced button
let mdc = document.querySelector(".popup-content"); //Pop up content block that changes
adv.onclick = () => {
mdc.innerHTML =
'<span class="close"></span>' + ' ... ' +
'<button class="back">Go back</button>' +
'<button class="closebtn">Go to site</button>'; //Here it creates same button as before
// I thought since it has the same class name it would work but it doesn't,
// tried with simple onclick function but it doesn't work either.
const back = document.querySelector(".back");
back.onclick = () => {
mdc.innerHTML =
'<span class="close"></span>' +
'<button class="adv">Advanced</button>' + //creates adv button again but doesn't work
'<button class="closebtn">Go to site</button>';
};
};
const xbtn = document.querySelector(".close");
const cbtn = document.querySelector(".closebtn");
const close = () => {
xbtn.onclick = () => {
mda.style.display = "none";
};
cbtn.onclick = () => {
mda.style.display = "none";
};
};
close();
I tried everything I could come up with, adding onclick=()
to the buttons, addEventListener
instead of onclick functions...
Also one weird thing that's happening is if i use getElementByClass
or Id
instead of query selector those close functions stops working... Please can someone guide me how am i supposed to do it correctly? BTW can't use any libraries.