Bind → target responds to clicks
Unbind → target no longer runs aClick
How It Works
Name. Store aClick as a shared function.
Bind..bind("click", aClick) attaches it.
Unbind. Pass the same aClick to remove it.
Example 2 — Unbind by Event Type
Remove every click handler, or wipe all handlers on the element.
jQuery
$("p").bind("click", function () {
$("#log").text("clicked");
});
$("p").unbind("click"); // all click handlers
// $("p").unbind(); // every handler type
After .unbind("click") → clicks do nothing
.unbind() with no args removes all types
How It Works
Attach. Bind a click handler.
Type..unbind("click") clears all click handlers.
Broad. Bare .unbind() clears every type.
Example 3 — Same Reference Required
Passing a new anonymous function does not remove the original handler.
jQuery
var foo = function () {
$("#log").text("foo ran");
};
$("button").bind("click", foo);
$("button").unbind("click", foo); // works
// Will NOT work — different function object:
// $("button").unbind("click", function () { … });
.unbind() removes event handlers attached with .bind(). You can remove all handlers, filter by event type, pass a specific handler reference, use namespaces, or pass an event object from inside a running handler.
Yes, but it has been deprecated since jQuery 3.0. Prefer .off() (available since 1.7) for new code.
Replace .bind(type, fn) with .on(type, fn) and .unbind(type, fn) with .off(type, fn). Namespace forms map the same way: .unbind(".myPlugin") → .off(".myPlugin").
JavaScript compares function references, not source text. .unbind("click", fn) needs the exact same fn passed to .bind(). Store the handler in a named variable.
.unbind() removes direct handlers from .bind(). .off() removes handlers from .on() — direct and delegated — and is the modern replacement for .unbind(), .undelegate(), and .die().
Bind with .bind("click.myEvents", fn), then .unbind("click.myEvents") removes only that binding, or .unbind(".myEvents") removes every handler in the namespace.