Unfortunately, this
doesn't seem to be pointing to the correct object in this scenario. I'm having trouble figuring out how to reference the right one.
function myObject() {
this.someMethod1 = function() {
var elementBtn = document.getElementById('myBtn');
elementBtn.onclick = function() {
this.anotherMethod2(); //I'd like to call this.anotherMethod2()
//...but it seems to be trying to call elementBtn.anotherMethod2() instead.
};
};
this.anotherMethod2 = function() {
alert('OK');
};
}
When the myBtn
button is clicked, I want myObject.anotherMethod2()
to execute. And I specifically want it to be that instance of myObject
, not any other. How can I achieve this?