There is a FileManager constructor, the setEventStatuses method is removed and the "change" event is set.
The problem is that when you call the setEventStatuses method again , the previous event on the inputs is not deleted and two "change" events are already hanging.
How can I delete the previous event? The problem, of course, is solved if you delete .bind (this) when hanging the event, but still I would like to save the this context in the changeStatus function.
Thank.

var FileManager = function () {}; FileManager.prototype.setEventStatuses = function () { var $el = $('#el'); // // ... // $el.off('change', 'input', this.changeStatus.bind(this)).on('change', 'input', this.changeStatus.bind(this)); }; FileManager.prototype.changeStatus = function (e) { // ... }; 
  • There are other change handlers on this input? - Grundy
  • @Grundy Not yet, but, of course, it would be interesting to assume that they may appear. - IDD

1 answer 1

The bind function each time returns a new function. And in the off function you need to pass the same function that was used in the on function.

Thus, the approach used in the query will not work with bind .

You can solve in several ways, for example, remove all change handlers from the input

 $el.off('change', 'input').on(...); 

Another option is to save somewhere the function that bind returns and use it.

  • That's it! Got it. Thanks for the detailed answer. - IDD