Lee Loftiss
Lee Loftiss

Reputation: 3205

unbind click event added to classes

I have the following line to add a click event to items with the same class:

jQuery('.multi_image').click(function(){onMultiImageClicked(jQuery(this));});

I am using the following code to try and unbind the click event:

jQuery('.multi_image').unbind('click');

However, the unbind does not seem to have any effect.

Is it possible to remove a click event from a class of objects like this?

Upvotes: 0

Views: 183

Answers (2)

Ionică Bizău
Ionică Bizău

Reputation: 113405

.unbind('click') must work... but I recommend to use on and off functions:

Use on and off functions instead:

jQuery('.multi_image').on("click", function (){
   onMultiImageClicked(jQuery(this));
});

and

jQuery('.multi_image').off("click");

From documentation:

The off() method removes event handlers that were attached with .on(). See the discussion of delegated and directly bound events on that page for more information. Specific event handlers can be removed on elements by providing combinations of event names, namespaces, selectors, or handler function names. When multiple filtering arguments are given, all of the arguments provided must match for the event handler to be removed.

Upvotes: 2

palerdot
palerdot

Reputation: 7642

From jQuery docs

Event handlers attached with .bind() can be removed with .unbind().

Since you are not using bind(), that might be the reason for this behavior. Try to use on(), off() or use bind() to bind the click handler before using unbind().

Upvotes: 0

Related Questions