there is a button that I am going to add to the page with innerHTML the button has an eventlistener on it onclick="addWatchlist()"
and in bellow, I am going to remove the event from the button by another event listener but I can't do it with removeEventListener("click",addWatchlist).
I have readen:
- why doesn't removeEventListener work?
- Javascript removeEventListener not working
they are about removing events that are added with javascript addEventListener.but here we add the event directly into HTML.
<button class="watch-list-btn" onclick="addWatchlist()">Watchlist </button>
watchlist.addEventListener("click",function(){
conatinerDown.innerHTML =""
conatinerDown.innerHTML +=movieWatchlistArray.map(each=> {
return each.outerHTML
}).join("")
// loop through movie list and changes text and removes event listener.
for(let i=0 ;i<moviePageNumber;i++ ){
document.querySelector(`.movie-${i} .watch-list-btn`).textContent = "remove"
document.querySelector(`.movie-${i} .watch-list-btn`).removeEventListener("click",addWatchlist)
}
})
// adds movie to start of an array called movieWatchlistArray
function addWatchlist(){
movieWatchlistArray.unshift(document.querySelector(`.movie-${moviePageNumber-1}`))
console.log(movieWatchlistArray)
}
is there a way to remove the event? without changing the HTML to something like :
<button class="watch-list-btn">Watchlist </button>