3

Please have a look of the following code snippet,

I have a method someFunc of the Javascript class someClass

Inside the onclick handler of the btn variable inside the method definition, I want to get a reference to the SomeClass and the button both.

SomeClass.prototype.someFunc = function(){

    var btn = document.crealeElement("button");
    btn.onclick = function() {
       this.somePropertyOfSomeClass = true; //this works, because bind
       this.classList.add("active"); //this does not
    }.bind(this);
}

I know, if I use bind(this) then the this variable inside the click handler refers to SomeClass, and without bind, it refers to the button element.

My problem is: How to get both? Because I want to some properties of the class SomeClass inside the handler

2
  • 1
    What's wrong with using btn.classList...? Commented Sep 6, 2018 at 9:18
  • @BenFortune didn't know we can do that too! Thanks Commented Sep 6, 2018 at 9:20

4 Answers 4

3

It's better and more modern practice to access the element (target) or current element the event is bubbling through (currentTarget). Using this is unclear inside a class for anything other than the class instance. Also it's better to use an event listener so multiple events can be attached of the same type.

SomeClass.prototype.someFunc = function(){
    const btn = document.crealeElement("button");
    btn.addEventListener('click', (event) => {
       this.somePropertyOfSomeClass = true; //this works, because arrow function has lexical scope
       event.target.classList.add("active"); // proper way to access the element
    }
}

Also you might want to take a look at ES6 classes.

Sign up to request clarification or add additional context in comments.

Comments

1

Just save the context in the outside function:

SomeClass.prototype.someFunc = function(){
   let _this = this;
   var btn = document.crealeElement("button");
   btn.onclick = function() {
      _this.somePropertyOfSomeClass = true; //saved context
      this.classList.add("active");
   }
}

Comments

1

Replace this code this.classList.add("active"); to btn.setAttribute("class", "active");

 SomeClass.prototype.someFunc = function(){

    var btn = document.crealeElement("button");
    btn.onclick = function() {
       this.somePropertyOfSomeClass = true; //this works, because bind
      btn.setAttribute("class", "active"); //this may be help you
    }.bind(this);
}

Comments

1

Something like this should work:

SomeClass.prototype.someFunc = function(){
    var that = this;

    var btn = document.crealeElement("button");
    btn.onclick = function() {
       that.somePropertyOfSomeClass = true; //references of SomeClass because of the closure
       this.classList.add("active"); //this should reference btn
    };
}

Also see @Dominic's post, that is an even better solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.