jQuery API

.unbind()

.unbind( [eventType] [, handler(eventObject)] ) Returns: jQuery

Description: Remove a previously-attached event handler from the elements.

  • version added: 1.0.unbind( [eventType] [, handler(eventObject)] )

    eventTypeA string containing a JavaScript event type, such as click or submit.

    handler(eventObject)The function that is to be no longer executed.

  • version added: 1.4.3.unbind( eventType, false )

    eventTypeA string containing a JavaScript event type, such as click or submit.

    falseUnbinds the corresponding 'return false' function that was bound using .bind( eventType, false ).

  • version added: 1.0.unbind( event )

    eventA JavaScript event object as passed to an event handler.

Event handlers attached with .bind() can be removed with .unbind(). (As of jQuery 1.7, the .on() and .off() methods are preferred to attach and remove event handlers on elements.) In the simplest case, with no arguments, .unbind() removes all handlers attached to the elements:

$('#foo').unbind();

This version removes the handlers regardless of type. To be more precise, we can pass an event type:

$('#foo').unbind('click');

By specifying the click event type, only handlers for that event type will be unbound. This approach can still have negative ramifications if other scripts might be attaching behaviors to the same element, however. Robust and extensible applications typically demand the two-argument version for this reason:

var handler = function() {
  alert('The quick brown fox jumps over the lazy dog.');
};
$('#foo').bind('click', handler);
$('#foo').unbind('click', handler);

By naming the handler, we can be assured that no other functions are accidentally removed. Note that the following will not work:

$('#foo').bind('click', function() {
  alert('The quick brown fox jumps over the lazy dog.');
});

// will NOT work
$('#foo').unbind('click', function() {
  alert('The quick brown fox jumps over the lazy dog.');
});

Even though the two functions are identical in content, they are created separately and so JavaScript is free to keep them as distinct function objects. To unbind a particular handler, we need a reference to that function and not a different one that happens to do the same thing.

Note: Using a proxied function to unbind an event on an element will unbind all proxied functions on that element, as the same proxy function is used for all proxied events. To allow unbinding a specific event, use unique class names on the event (e.g. click.proxy1, click.proxy2) when attaching them.

Using Namespaces

Instead of maintaining references to handlers in order to unbind them, we can namespace the events and use this capability to narrow the scope of our unbinding actions. As shown in the discussion for the .bind() method, namespaces are defined by using a period (.) character when binding a handler:

$('#foo').bind('click.myEvents', handler);

When a handler is bound in this fashion, we can still unbind it the normal way:

$('#foo').unbind('click');

However, if we want to avoid affecting other handlers, we can be more specific:

$('#foo').unbind('click.myEvents');

We can also unbind all of the handlers in a namespace, regardless of event type:

$('#foo').unbind('.myEvents');

It is particularly useful to attach namespaces to event bindings when we are developing plug-ins or otherwise writing code that may interact with other event-handling code in the future.

Using the Event Object

The third form of the .unbind() method is used when we wish to unbind a handler from within itself. For example, suppose we wish to trigger an event handler only three times:

var timesClicked = 0;
$('#foo').bind('click', function(event) {
  alert('The quick brown fox jumps over the lazy dog.');
  timesClicked++;
  if (timesClicked >= 3) {
    $(this).unbind(event);
  }
});

The handler in this case must take a parameter, so that we can capture the event object and use it to unbind the handler after the third click. The event object contains the context necessary for .unbind() to know which handler to remove. This example is also an illustration of a closure. Since the handler refers to the timesClicked variable, which is defined outside the function, incrementing the variable has an effect even between invocations of the handler.

Examples:

Example: Can bind and unbind events to the colored button.

<!DOCTYPE html>
<html>
<head>
  <style>
button { margin:5px; }
button#theone { color:red; background:yellow; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="theone">Does nothing...</button>
<button id="bind">Bind Click</button>
<button id="unbind">Unbind Click</button>

<div style="display:none;">Click!</div>
<script>

function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
// could use .bind('click', aClick) instead but for variety...
$("#theone").click(aClick)
  .text("Can Click!");
});
$("#unbind").click(function () {
$("#theone").unbind('click', aClick)
  .text("Does nothing...");
});

</script>

</body>
</html>

Demo:

Example: To unbind all events from all paragraphs, write:

$("p").unbind()

Example: To unbind all click events from all paragraphs, write:

$("p").unbind( "click" )

Example: To unbind just one previously bound handler, pass the function in as the second argument:

var foo = function () {
// code to handle some kind of event
};

$("p").bind("click", foo); // ... now foo will be called when paragraphs are clicked ...

$("p").unbind("click", foo); // ... foo will no longer be called.

Support and Contributions

Need help with .unbind() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .unbind()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://twitter.com/bigredswitch bigredswitch

    you know what would be awesome? .rebind() … esp for namespaced events

  • http://twitter.com/aivopaas Aivo Paas

    It would also be useful to have .unbind(events) where events is a map of event names and functions. Just like .bind(events) in 1.4

  • http://www.cnblogs.com/zhangziqiu ZQ

    unbind multiple namespaced handlers don't work:
    $(“#div1″).bind(“click.myName”, function(e) { alert(“1″); });
    $(“#div1″).bind(“click.yourName”, function(e) { alert(“3″); });
    $(“#div1″).unbind(“click.myName.yourName”);//still alert 1 and 3

  • mauzer_tim

    It seems that unbind('eventType', handler) unbinds all handlers for this event type and not only this handler.

    I have 2 objects each of them subscribe itself to document's “mousedown” event:

    $(document).bind('mousedown', this.onMouseDown);

    However when one of the objects unsubscribes itself with this command:
    $(document).unbind('mousedown', this.onMouseDown);
    another object stops receiving 'mousedown' events (it's onMouseDown callback stops being called).

    I'm using 1.4.2 version, 1.3.2 had the same behavior. Is it the correct behavior or it's a bug?

  • Jaffa Tonga

    $(document).unbind(‘mousedown’, this.onMouseDown);
    will unbind all event ‘mousedown’ under document, if you want to unbind only one ‘mousedown’ you unbind that specific one and not using document
    something like $(textbox2).unbind(‘mousedown’,this.onMouseDown);

  • Jaffa Tonga

    $(“#div1″).unbind(“click.myName.yourName”); will only unbind the one below
    $(“#div1″).bind(“click.myName.yourName”);
    if you want to unbind the click.myName you use this
    $(“#div1″).unbind(“click.myName”, function(e) { alert(“1″); });
    if you want to unbind the click.yourName you use this
    $(“#div1″).unbind(“click.yourName”, function(e) { alert(“3″); });

  • mauzer_tim

    The documentation doesn't mention any difference in behavior of “unbind” depending on the kind of element that unbind is calling for. Moreover it would be illogical as in this case there is no need in the second argument of “unbind”.

  • Algy

    Unbinding a map of events seems to work (1.4.2)

    $(“#elem”).unbind({“mouseenter”: handler, “mouseleave”: handler });

    you can check with: console.dir($(“#elem”).data(“events”));

  • Ryan

    It would be awesome if unbind returned the object it was binded to, so that a bind could more easily be restored later.

  • Chris

    Does it return the current object right now? One of the things I’m doing with my code now (or hoping to do) is use this pattern:
    object.unbind().bind(…)

    Though I’m not sure it’ll work on freeing event handlers from dynamically generated elements, which is the purpose…

  • http://blog.wassupy.com/search/label/Technology Michael Haren

    mauzer_tim–this is how jquery works–select-act, select-act, select-act. It's a very, very common pattern

  • inspire22

    How do you unbind a binding added with .live?

  • Guest

    .die();

  • http://twitter.com/chadhikes Chad Killingsworth

    The description mentions that unbind can be called without arguments, but the Raw XML doesn't seem to reflect that.

  • http://www.learningjquery.com/ Karl Swedberg

    Thanks, Chad. I added the “optional” flag to the first signature's arguments in the XML.

  • http://twitter.com/Chestozo Roman

    Unbind is not working is case we are working with a jQuery object, created as a proxy to object that is not a DOM element.

    I mean this situation:
    var $o = $({});
    var h = function(){ alert('Here!'); };
    $o.bind(“test_event”, h);
    $o.trigger(“test_event”);
    $o.unbind(“test_event”);

    Results in “a.removeEventListener is not a function”.

    Is this a known bug/issue or am I the first? )

  • Ian Grainger

    Found a little gotcha that you cannot unbind callback functions passed in as options when creating a (for e.g.) widget.

    So $('#elem').unbind('autocompleteselect'); will not work if you use:
    $('#elem').autocomplete({select: function});
    But will if you use:
    $('#elem').bind('autocompleteselect', function);

    I thought this was a bug with unbind which I'd logged here: (http://bugs.jqueryui.com/ticke…).

  • Ian Grainger

    Just been pointed out to me that this is specific jQuery-ui behaviour, and as such this may not be the right place for this comment.

    If this is deemed not relevant, please remove my comment.

  • Harish Uce2009

    I have fn like this how to change?

    acfb = jQuery(“ul.first”).autoCompletefb({urlLookup:data});