0

I'm writing a web app that has a simple button. In my d3.js code, I attach a listener function, so that when the button is clicked, I can perform some actions.

I've also written a function in my d3.js code that performs an alert.

The problem is that while the onclick listener works (it successfully calls an alert), it doesn't call the function that I wrote. Here's my code:

  do() {
    alert("ha");
  }

  componentDidMount() {
    d3.select("body").select("#filterButton").on("click", function() {
      alert("hi");
      this.do();
    });
  }

The alert inside the onclick works, but do() isn't called (I'm not getting a second alert). Why is this so?

Mark Siano
  • 23
  • 1
  • 1
  • 3
  • 3
    In your function, `this` does not reference the global object but your d3 selection, so here `this.do` is undefined. It will work if you simply call `do()`. (But it's not a good idea to structure your program this way, I assume this is just for the sake of the example.) – Nicolas Le Thierry d'Ennequin Jul 12 '17 at 18:56
  • Thanks, that makes sense. – Mark Siano Jul 13 '17 at 18:04

0 Answers0