I am using JQuery .live to listen for a custom event, ie not .click, .change, .keyup or any of the native events, but rather an event that I create using jquery .trigger. I have several places in the application that listen for this event using .live. I want to remove a single handler from the event. None of the methods I’ve tried seem to work including, .unbind, .die, and .undelegate. I’m using jquery 1.6 so I can’t use .off. What’s the best way to accomplish this?
This is essentially the form my code takes. I call unbind first to remove the handler if it already exists.
OBJECT 1
{
myNameSpace.MY_CUSTOM_EVENT_STRING = "my_event";
$cachedFieldObject.trigger(myNameSpace.MY_CUSTOM_EVENT_STRING);
}
OBJECT 2
{
function myfunctionName(var1) {myObject.myObjectsFunction()}
$("#divID").find(".fieldClass").unbind(myNameSpace.MY_CUSTOM_EVENT_STRING, myfunctionName).live(myNameSpace.MY_CUSTOM_EVENT_STRING, myfunctionName);
}
live() attaches delegated event handlers to the document element so you’ll have to unbind them from the document element this fiddle shows how to unbind specific functions from live but not specific selected elements.
EDIT:
From the doc
It is possible to unbind custom events, but you have to do it for all bound elements
In this fiddle the custom event only becomes unbound when all elements are unbound.