-1

So im using... even don't know how to call this feature, but it looks like variable/id for events.

So in jquery i can do like this

    $('.button').off('.buttonClick').on('click.buttonClick', function() {
         console.log("blahblahblah")
     })

So first i remove specific event(.buttonClick) from this button then add.

This helps me prevent leaks from some Libraries that we are using.

How to do something like this in vanilla javascript?

==========Added==========

The point is removing a specific same event listeners. So for example if i have two different 'click' event listeners, i will remove only that what i need to remove, but not others. So if i type

 $(document).on('click.docClick1', function() {
     console.log("1")
 })
  $(document).on('click.docClick2', function() {
     console.log("2")
 })

$(document).on('click.docClick3', function() {
     console.log("3")
 })

And then type

$(document).off('.docClick2')

First and third 'click' event listeners will remain.

Nodge
  • 1
  • 1
  • Does this answer your question? [Is it possible to remove all event handlers of a given element in javascript?](https://stackoverflow.com/questions/2837542/is-it-possible-to-remove-all-event-handlers-of-a-given-element-in-javascript) – GrafiCode Oct 09 '22 at 11:20

1 Answers1

0
document.querySelector('.buttonClick').removeEventListener('click', functionListener);
document.querySelector('.button').addEventListener('click', function {
             console.log("blahblahblah");
})
Haim Abeles
  • 982
  • 6
  • 19