I have an object like the following:
class obj{
constructor(name){
this.name = name
}
update(){
console.log(this.name)
}
}
then I have an array of those objects, let’s say:
var array = [new obj("A"), new obj("B"), new obj("C")]
How do I programmatically create a button for each one of the objects and link the onclick function of each button to a different object of the array?
In my head this was something like:
for (var i = 0; i < array.length; i++) {
var button = document.createElement("button")
button.innerText = array[i].name
button.addEventListener("click", array[i].update)
}
But this doesn’t work.
Thank you.