I want to create multiple buttons that when clicked pass their object into the function (id for example), but what I get is button passes window object when clicked.
for(var n = 0; n < 10; n++){
var button = document.createElement("button");
button.textContent = n;
document.getElementById("test").appendChild(button);
button.onclick = () => {
console.log(this)//returns window object instead of button :(
console.log(this.id)
}
}
I searched and found no exact answer to why it could happen or how I can fix this. If I manually add onClick with function(this) it works like a charm