i'm trying to loop an HTMLCollection and i want to add a onclick event inside each element.
for(var page of pages){
page.onclick = () => {
console.log(count)
}
count += 1
}
Well, the problem is actually that when i click the button, it shows up only the last counted number (5), but not actually the counted element. How to fix it? Thanks.
for(var page of pages){
page.onclick = () => {
console.log(count++)
}
}
I guess this is what you want ? increment after a click ? and not in the for loop
One way would be to use a for Loop.
for (let count = 0; count < pages.length; count++) {
pages[count].onclick = () => {
console.log(count)
}
}