0

I'm completely new to javascript so my terminology is probably a bit off but here we go.

So I want to use attributes in my onClick but closures really confuses me. How would i go about doing for example:

class Foo {
    constructor(bar) {
        this.bar = bar;
        this.localElement = document.createElement("div");
        this.localElement.onClick = this.onClick;
    }
    
    onClick(e) {
       alert(bar);
    }
} 

What i want to achieve is that each instance of Foo will be connected to an element that can use the attributes of Foo when clicked on. Obviously this code doesn't work but how would I go about achieving that? I suspect my thought process is a bit too object oriented so I'd appreciate any tips.

0 Answers0