javascript - Find object from dom element on event -
i have created instances of object. 1 function in object creates li
element , uses addeventlistener("click", this, false)
. when event handler runs can show objects instance variables etc. know accessing proper instance of object , running event handler.
so there way trace dom element instance of object created it. in case this
providing link.
what element selector or id. somehow instance of object. find out this
element (not referring dom element this
) thinking workaround create custom event , return object in handler function. element , run myelement.click()
.
is there better way this?
here's basic flavor, making use of this
:
const obj = { name: "my object", listen() { someelement.addeventlistener('click', this.listener.bind(this)); }; listener(event) { console.log("the event , name are", event, this.name); } };
you use handleevent
approach:
const obj = { name: "my object", handleevent() { console.log("i clicked!", this.name); }, listen() { someelement.addeventlistener('click', this); } };
Comments
Post a Comment