1

I need help calling the this.load function from inside the thediv.onclick. I stripped out most of the code so its really basic, but I really can't find a way to do it. Here is what I currently have:

function CreatePizza(Name, Toppings) {
  this.n = Name;
  this.t = Toppings;
  this.load = function loadpizza() {
    //function that i want to be called
  }

  this.create = function button() {
    var thediv = document.createElement("div");
    thediv.onclick = function() {
      // Call this.load function here
    }
  }
}
4
  • What happens if you just call this.load() inside of the thediv.onclick function? Commented Jan 27, 2016 at 16:32
  • @MatthewHerbst because the function gets it's own context, the this value will have changed. Simply assign it to a variable and call it from that variable. Commented Jan 27, 2016 at 16:34
  • @somethinghere I know - I wanted OP to show us he was trying so he could learn :) Commented Jan 27, 2016 at 16:37
  • @MatthewHerbst Sorry about that then, but to be honest, if you read your comment again it does sound an awful lot like a genuine question and not a brain-teaser :) Commented Jan 27, 2016 at 16:38

5 Answers 5

6

The problem is that inside the onclick handler, this will refer to the <div>, not the other this which you refer to repeatedly.

Two possible solutions:

  1. Save a reference to your desired this:

    that = this;
    thediv.onclick = function () {
       that.load()
    };
    
  2. Bind this to your function:

    thediv.onclick = function () {
        this.load();
    }.bind(this);
    

    Or, if that's the only thing you're doing in that function anyway:

    thediv.onclick = this.load.bind(this);
    
Sign up to request clarification or add additional context in comments.

3 Comments

Good answer for showing both of the two main ways to solve this common problem.
Good answer, I forgot about the binding here. It wasn't immediatly the issue, but I guess it's next on the list after solving this :)
Thank you so much, really nice awnser!
2

Because of closures, you can simply assign this to a variable and call it from that!

function CreatePizza(Name, Toppings) {
  var self = this;
  this.n = Name;
  this.t = Toppings;
  this.load = function loadpizza() {
    //function that i want to be called
  }
  this.create = function button() {
    var thediv = document.createElement("div");
    thediv.onclick = function() {
      self.load();
    }
  }
}

I would like to mention that a nicer - and not necessarily better, before anyone starts a flamewar - way to attach events to your div (and more elegant in my opinion) is to use thediv.addEventListener('click', self.load, false). Just a side-note, though.

Comments

1

Backup the this object before binding the event.

this.create = function button() {
    var that = this,
        thediv = document.createElement("div");

    thediv.onclick = function() {
        // Call this.load function here
        that.load();
    }
}

Comments

0
function CreatePizza(Name, Toppings) {
  this.n = Name;
  this.t = Toppings;

  var foo = function loadpizza() {
    //function that i want to be called
  };


  this.load = foo;
  this.create = function button() {
    var thediv = document.createElement("div");
    thediv.onclick = function() {
      foo();
    }
  }
}

2 Comments

I think you mean this.load = foo and not foo() as that will call your function.
actually reading @Sprotte's answer I think I misunderstood the question, I will leave this up just in case however
0
function CreatePizza(Name, Toppings) {
  this.n = Name;
  this.t = Toppings;
  this.load = function loadpizza() {
    //function that i want to be called
  }
  var self = this;
  this.create = function button() {
    var thediv = document.createElement("div");
    thediv.onclick = function() {
      self.load()
    }
  }
}

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.