Reputation: 33
I have a situation where Jquery Live() function need to be disable/unbind if it is already exist on the page.
Let's say I've two components, each component is one jsp and both have same javascript function.
JS:
(function(a) {
a(function() {
a(".show").live(
"click", function(){
}); })(jQuery);
If I drag and drop first component it is working fine but when I drop second component...existing component functionality is not working since it has duplicate JS and selectors..
Is there a way that I can unbind or disable jquery live() for the second component when I drag and drop.
Appreciate your help...
Upvotes: 1
Views: 130
Reputation: 781721
To remove a binding that was created using .live()
, use .die()
:
$(".show").die("click");
If you bound the event to a named function, you could remove just that one handler by passing the function in as the second argument to .die()
. But since you're binding to an anonymous function, you can't provide that as the argument, so all click handlers bound using .live()
will be removed.
See http://api.jquery.com/die/ for more information. Also, .live()
was deprecated in jQuery 1.7, and removed in 1.9. You should convert your code to use .on()
.
It's possible you don't need to do this in the first place, though. Why are you calling .live()
multiple times? You only need to call it once, and it will apply to all elements matching the selector, even if they're added later. That's the reason to use .live()
or .on()
, rather than .bind()
.
Upvotes: 1