Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a class method as onclick handler in JavaScript?

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.

like image 379
adaj21 Avatar asked Oct 27 '13 21:10

adaj21


People also ask

Can you call multiple functions inside of the onclick handler?

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 do you use onClick event in JavaScript?

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.

What is onclick handler in react?

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.

How to assign a JavaScript function to an HTML click event?

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.


2 Answers

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 causes this to be set incorrectly for inner functions.

See also What does 'var that = this;' mean in JavaScript?

like image 171
Nils Lindemann Avatar answered Sep 18 '22 01:09

Nils Lindemann


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 this 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.

like image 26
zastrowm Avatar answered Sep 19 '22 01:09

zastrowm