Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to remove one specific event handler from an event with multiple handlers?

Tags:

jquery

As i searched for the solution of a problem i have right now, i found this thread: jQuery: more than one handler for same event. Now i wonder if it is possible to remove just a single one of these handlers?

like image 761
marue Avatar asked Feb 27 '11 22:02

marue


People also ask

Can event handlers be removed?

Go to the objects tab, view the Document object (don't click on edit) and scroll down to Event Handlers. Select the one to delete and press delete.

Can you have multiple event handlers?

You can assign as many handlers as you want to an event using addEventListener(). addEventListener() works in any web browser that supports DOM Level 2.

Which event handler method would be used to remove an event handler that was attached with ON ()?

Using off() Method: It is used to remove event handlers attached with the on() method.

Which function is used to remove an existing event handler?

The removeEventListener() method removes an event handler from an element.


2 Answers

It is possible, if you don't use anonymous callbacks:

var $elt = $(...);  function clickHandler1(event){...} function clickHandler2(event){...}  // bind both $elt.click(clickHandler1); $elt.click(clickHandler2);  // unbind just the first $elt.unbind('click', clickHandler1); 

A wild Demo appeared!

See also: .unbind() docs.

like image 114
Matt Ball Avatar answered Sep 27 '22 21:09

Matt Ball


Actually, you can do it using anonymous callbacks via event namespacing:

$elt.bind('click.handler1', function(event) {     alert('click handler 1'); });  $elt.unbind('click.handler1'); 

See modified demo here.

And here for more information.

This was added in jQuery 1.4.3, which actually came out more than 4 months before the question was asked.

like image 31
Kevin Jurkowski Avatar answered Sep 27 '22 21:09

Kevin Jurkowski