Started to use addEventListener some weeks ago. It is one case i cant understand so far. We are talking about cases where we printing taggs with templet literals and declaring the class inside thos taggs. After that if we going to use thise classes for other EventListener, somethimes we need to have other EventListener inside the first one(the one who is printing/declaring classes) other times we dont have to have another eventListener inside the first one. Why? Somebody have good explanation or can point me in right direction?
I am giving exemples under.
Thank you in advance
//two different Event Listener were the first one making class and second one using that class
const drink = document.querySelector(".drink");
let drinkTxt = "";
MenyModule.getDrink().forEach(drink =>{
drinkTxt +=`
<div class="boks">
<h4>${drink.name}</h4>
<p class="des">${drink.des}</p>
<p class="price">kr ${drink.price}</p>
<a class="button-buy">BUY</a>
</div>
`
});
drink.innerHTML = drinkTxt;
document.querySelectorAll(".boks").forEach(boks =>{
boks.addEventListener("click", (e)=>{
alert();
})
});
//Second exemple where i need to have other EventListener inside the first one or alert() will not be executed
document.querySelectorAll(".button-buy").forEach(button =>{
button.addEventListener("click", ()=>{
valgteRetterTxt+=`<a class="remove1">REMOVE</a>`
valgteRetter.innerHTML=valgteRetterTxt;
document.querySelectorAll(".remove1").forEach(remove=>{
remove.addEventListener("click", ()=>{
alert();
});
});
});
});