unbind function not working
in Using jQuery
•
12 years ago
Hi
I'm having trouble with unbinding an event, hopefully someone can enlighten me. The code below runs when the form is ready. If I bind a click event to all elements using the $("*").bind the unbind() of a particular element does not work. If I bind to the particular element as in $("#theText").bind("click", f); then the unbind does work.
I also tried unbinding all events on that element using unbind with no args as well as unbinding just the particular event, doesn't seem to make any difference. The event stays on the element.
Is there something obvious I'm missing about this method?
Thanks
Joe
var f = function(event) {
$("#console").append(this.tagName + " " + this.id + "<br />");
if (this.id == "stopBubble")
event.stopPropagation();
}
// bind above function all elements click events.
$("*").bind("click", f); //if I bind to all elements the unbind below doesn't work
$("*").bind("click", f); //if I bind to all elements the unbind below doesn't work
//$("#theText").bind("click", f); //if I bind explicitly to this element the unbind does work
$("#theText").unbind("click");
1