When I create a couple of buttons using forEach then add an event listener to each and then try to access the element itself inside that listener, it just defaults to the last one.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<template id="button-template">
<button></button>
</template>
<script src="./bug.js"></script>
</body>
</html>
Js:
const button_template = document.getElementById("button-template")
for(let i = 0; i < 10; i++) {
clone = button_template.content.cloneNode(true)
btn = clone.querySelector("button")
btn.addEventListener("click", () => {
btn.style.display = "None"
})
btn.innerHTML = i.toString()
document.querySelector("body").appendChild(clone)
console.log("something")
}
In this example, I try to make each button remove itself. But all of them just remove the tenth one. How can I fix this? I already tried using this or selfbut I get the same result