I'm making a Watchlist app. I got movies via API, rendered them on the page, using fetch/.then methods inside a for loop.
Then after the for loop I try to grab all the buttons "remove" on each movie and add event listener to them, but it doesn't work. Could you please help to correct my bug?
for (let i = 0; i < movies.length; i++) {
fetch(`https://www.omdbapi.com/?i=${movies[i]}&apikey=123`)
.then(res => res.json())
.then(movie => {
containerList.innerHTML += `<div class="movie"></div>})
//here outside of the loop I write the code that doesn't work
document.querySelectorAll(".buttonClass").forEach(button => {
button.addEventListener("click", () => {
localStorage.clear()
containerList.innerHTML = ""
})
})
The last part of code doesn't work, maybe because it's asynchronous and I haven't indicated it in the code. Should I use .finally in this case?
Here's the full page code (only this particular page, there is also the 2d one with the search field): https://codepen.io/btb8293/pen/KKoLOmZ
Thank you in advance!