Consider the following Javascript class:
function ClassA() {
this.someVariable = someValue;
this.myHandler = function(){
// I WANT TO CALL InnerFunction
this.innerFunction();
};
this.innerFunction = function(){
// do something that accesses/manipulates someVariable
};
this.hookUp = function(id){
document.getElementById(id).onclick = this.myHandler;
};
};
...
...
var button = document.createElement('button');
button.setAttribute('id','mybuttonid');
// append button to the document
...
...
var x = new ClassA();
x.hookUp('mybuttonid');
When I click on the button, the handler executes, however, 'this' now refers to the button element instead of the ClassA object, so it cannot resolve innerFunction().
What i need to have is a way to indicate to the handler that the context of this is the instance of ClassA (similar to $.ajax({context: this....}) where you can use 'this' in the .done() or .error() handlers), or a way to pass a reference to the instance to the handler without making the handler execute at instantiation time. For example if I try to pass 'this' as a praremter to myHandler (myHandler=function(ref){}, and then change : document.getElementById(id).onclick = this.myHandler(this);) But as you add parameters to myHandler, the function is executed at class instantiation time instead of at click time.
Any help will be greatly appreciated.
Similarly, calling multiple functions inside of the onClick handler is also possible: Again, the code above is not as readable as it could be if we just called the sayHello function and set the state inside of that. However, there may be instances when you need to do the above.
How to Use the onclick event in JavaScript The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you change certain content on the web page, and other things like that. You place the JavaScript function you want to execute inside the opening tag of the button.
In React, the onClick handler allows you to call a function and perform an action when an element is clicked. onClick is the cornerstone of any React app.
In HTML, we can use the onclick attribute and assign a JavaScript function to it. We can also use the JavaScript's addEventListener () method and pass a click event to it for greater flexibility.
Replace...
this.myHandler = function(){
this.innerFunction();
};
... with ...
var self = this;
this.myHandler = function() {
self.innerFunction();
};
See this article by Crockford. Cite:
By convention, we make a private
that
variable. This is used to make the object available to the private methods. This is a workaround for an error in the ECMAScript Language Specification which causesthis
to be set incorrectly for inner functions.
See also What does 'var that = this;' mean in JavaScript?
Like you already discovered, the value of this
depends on how the method is called. Thus, it can be confusing when using this
within event handlers attached to DOM elements.
Instead, when adding handlers, use an an anonymous method along with a variable within a closure.
For example, change your hookup function to be:
this.hookUp = function(id) {
var _this = this;
document.getElementById(id).onclick = function() {
_this.innerFunction();
}
}
Notice that you no longer depend on thi
s from within the onclick handler and thus avoid the problem. Once you're in innerFunction
, you can continue to use this
as you normally would, as it now correctly points to the correct object.
For a deeper explanation on how this
works in functions, MDN has a nice article about the this
keyword.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With