0

I'm having difficulties removing an event listener, since removeEventListener only works for non-anynomous functions I need to make a named function. The problem is I can't figure out how to pass the chart variable to the toggleLabels function without calling the function while I do it..

I tried looking at this question but no answer worked.

Is this possible?

var chart = {}; // Big object 

labelToggle.addEventListener('click', toggleLabels);

function toggleLabels(chart) {

    scope.graph.isLabelsVisible = chart.isLabelsVisible = !chart.isLabelsVisible;

    for (i = 0; i < length; i++) {
      chart.series[i].isLabelsVisible = !chart.series[i].isLabelsVisible;
      chart.series[i].hide();
      chart.series[i].show();
    };
  };
Community
  • 1
  • 1
Chrillewoodz
  • 27,055
  • 21
  • 92
  • 175
  • I usually use event listeners in the body of the html, and there, you can pass the arguments directly (`onclick='foo("toto")'` for example) – HolyDanna Jul 28 '16 at 12:29
  • @HolyDanna That's horrible practice, and I can't do that unfortunately. – Chrillewoodz Jul 28 '16 at 12:30
  • I know, but I try not to use much event listeners, any way. Maybe this post ( http://stackoverflow.com/questions/256754/how-to-pass-arguments-to-addeventlistener-listener-function#256763 ) can help you find a solution – HolyDanna Jul 28 '16 at 12:32

5 Answers5

1

When passing parameters, you can use an "anonymous function" that calls the specified function with the parameters:

labelToggle.addEventListener("click", function() {
    toggleLabels(chart);
});

var a = 5;
var b = 7;
document.getElementById('btn').addEventListener("click", function() {
    myFunction(a, b);
});

function myFunction(x, y) {
    var result = x + y;
    console.log(result);
}
<button id="btn">Click here!</button>
Kld
  • 6,970
  • 3
  • 37
  • 50
0

What you can do, is to make toggleLabels a function of chart. For example:

function chart(series) {
    this.series = series;  
    this.toggleLabels = function (name) {
    for (i = 0; i < lthis.series.ength; i++) {
      chart.series[i].isLabelsVisible = !this.series[i].isLabelsVisible;
      this.series[i].hide();
      this.series[i].show();
    };
    };
}

And then just operate on the chart object like this:

chart.series
chart.toggleLabels()

And then, you should be able to do the following:

 labelToggle.addEventListener('click', chart.toggleLabels);
uksz
  • 18,239
  • 30
  • 94
  • 161
0

You can return a function like this:

var chart = {}; // Big object 

labelToggle.addEventListener('click', toggleLabels(chart));

function toggleLabelsCls(chart) {

    return function(chart) {
       scope.graph.isLabelsVisible = chart.isLabelsVisible = !chart.isLabelsVisible;

       for (i = 0; i < length; i++) {
         chart.series[i].isLabelsVisible = !chart.series[i].isLabelsVisible;
         chart.series[i].hide();
         chart.series[i].show();
       };
     }
  };
CMPS
  • 7,733
  • 4
  • 28
  • 53
0

It can be done by using partial application:

labelToggle.addEventListener('click', toggleLabels.bind(this,chart));
Igorsvee
  • 4,101
  • 1
  • 25
  • 21
0

Here the shortest solution:

labelToggle.addEventListener('click', function(){toggleLabels(chart)});
Yan Pak
  • 1,767
  • 2
  • 19
  • 15