I'm writing a simple js class that looks for a div and injects into it an img tag.
I need to call an internal function stop() if the user click on the div.
class PreDS {
constructor() {
console.log("[PreDS] constr")
this._el = document.querySelector(".imgOrari")
if (this._el){
this._el.innerHTML = "<img id='imgOrariImg'>";
this._el.onclick =
function(){
console.log("[PreDS] click _el")
--> stop("click")
}
}
else console.error("class imgOrari not found")
}
stop(){
...
}
The problem is that the onclick handler defined like this, is not in the context of the object, so the function is undefined.
How can i recall the function?