Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JS/TS add/remove event listener with an defined event handler
class UseDefinedEventHandler extends TestSetting {
public addEventListener() {
this.removeEventListener();
if (this.element == null) {
return;
}
this.element.addEventListener("click", this.onClickEventHandler);
}
public removeEventListener() {
if (this.element == null) {
return;
}
// removes the listener succesfully
this.element.removeEventListener("click", this.onClickEventHandler);
}
private onClickEventHandler(event: Event): void {
// 'this' is no more the instance of the class
console.log(this);
}
}
const useDefinedEventHandler = new UseDefinedEventHandler();
useDefinedEventHandler.addEventListener();
useDefinedEventHandler.addEventListener();
useDefinedEventHandler.simulateOnClick();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment