Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overwrite event handler for 'click' event in javaScript?

Tags:

javascript

I want to overwrite event handler for click event. This is the event handler I have attached initially.

document.querySelector("[data-id='start-btn']")
            .addEventListener("click", function (evt) {
                //some code
            });

Again after some condition, I want to over write this handler and attach new for 'click' event.

 //removing
 document.querySelector("[data-id='start-btn']")
    .removeEventListener("click", function (evt) {
            //some code
        }, false);

  //attaching new
  document.querySelector("[data-id='start-btn']")
    .addEventListener("click", function (evt) {
            //code
        });

But still it is executing the previous event handler.I have used removeEventListener(but I guess, its not working).

Guide me where I am going wrong.

like image 651
Neha Gupta Avatar asked Mar 19 '16 18:03

Neha Gupta


People also ask

Does addEventListener overwrite onClick?

Note: The addEventListener() method can have multiple event handlers applied to the same element. It doesn't overwrite other event handlers. Example: Below is a JavaScript code to show that multiple events are associated with an element and there is no overwriting.

Does addEventListener overwrite?

addEventListener does not overwrite existing event listeners, it simply adds a new one as the method name implies. Existing listeners must be removed using the removeEventListener method.

How do you delete a click event in an element?

The removeEventListener() is an inbuilt function in JavaScript which removes an event handler from an element for a attached event. for example, if a button is disabled after one click you can use removeEventListener() to remove a click event listener.

Can event handlers be removed?

Clear the handlerTo remove an event handler, you can't just delete handler code that is in the form's code-behind file, it's still referenced by the event.


2 Answers

The only way to remove a handler added with addEventListener is to use removeEventListener with exactly the same arguments. That means you'll need a reference to the original function:

var handler = function (evt) {
    //some code
};
document.querySelector("[data-id='start-btn']").addEventListener("click", handler);

then to remove

document.querySelector("[data-id='start-btn']").removeEventListener("click", handler);
like image 147
T.J. Crowder Avatar answered Oct 20 '22 02:10

T.J. Crowder


removeEventListener makes sense really only when using function references rather than passing an entire function body to both it and addEventListener, which would potentially mean mass duplication of code (and, as you've found, doesn't work anyway.)

So, prepare a reference to your function:

function my_func() { /* code */ }

And pass it as the handler argument to add/removeEventListener

document.querySelector('query').addEventListener('click', my_func);
document.querySelector('query').removeEventListener('click', my_func);

There is an easier way that utilises an older coding standard. If you specifically want only one event handler for a given type and element, you can use the DOM-zero onclick.

document.querySelector('query').onClick = my_func;
document.querySelector('query').onClick = my_func2; /* my_func() will no longer fire */
like image 38
Mitya Avatar answered Oct 20 '22 02:10

Mitya