How to remove delegates from multiple events using a single undelegate method in jquery

Can I make the following much simpler (instead of using undelegate twice)?

$("#div1").undelegate("div", "mouseenter").undelegate("div", "mouseleave"); 

I do not want event handlers other than mouseenter and mouseleave to violate.

+7
source share
2 answers

Separate your events with spaces.

 $("#div1").undelegate("div", "mouseenter mouseleave"); 

You should use on and off though.

 $("#div1").off("mouseenter mouseleave", "div"); 

http://api.jquery.com/undelegate/

The .undelegate () method is a way to remove event handlers that have been associated with using .delegate (). Starting with jQuery 1.7, .on () and .off () methods are preferred for attaching and removing event handlers.

+17
source

In this particular case, you can use the abbreviated .hover() for mouseenter and mouseleave .

 ​$("#div1").off("hover", "div"); 

.off() is the recommended way to remove event handlers from jQuery 1.7.

+1
source

All Articles