I want to create a dynamic HTML/JS user interface. For this I will dynamically create objects, which will be represented as a button on my html. now, I want to track click events of course. How do I dynamically add an addEventlistener to this special instance of the button? (each button creates its own svg icon) My idea is, that the object IS the button, but I am not sure how to do that...
my idea is like following:
class Lightbulb {
/**
*
* @param {String} id
* @param {boolean} state
*/
constructor(id, state){
this.id = id;
this.state = state;
/*
create the icon here...
*/
//is it somehow possible to achieve the following?
this.addEventlistener("click", this.setState(!this.state));
}
/**
*
* {boolean} newState
*/
setState(newState){
console.log(newState)
}
``