jQuery API

.die()

Contents:

.die() Returns: jQuery

Description: Remove all event handlers previously attached using .live() from the elements.

  • version added: 1.4.1.die()

Any handler that has been attached with .live() can be removed with .die(). This method is analogous to calling .unbind() with no arguments, which is used to remove all handlers attached with .bind(). See the discussions of .live() and .unbind() for further details.

As of jQuery 1.7, use of .die() (and its complementary method, .live()) is not recommended. Instead, use .off() to remove event handlers bound with .on()

Note: In order for .die() to function correctly, the selector used with it must match exactly the selector initially used with .live().

.die( eventType [, handler] ) Returns: jQuery

Description: Remove an event handler previously attached using .live() from the elements.

  • version added: 1.3.die( eventType [, handler] )

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

    handlerThe function that is no longer to be executed.

  • version added: 1.4.3.die( eventTypes )

    eventTypesA map of one or more event types, such as click or keydown and their corresponding functions that are no longer to be executed.

Any handler that has been attached with .live() can be removed with .die(). This method is analogous to .unbind(), which is used to remove handlers attached with .bind(). See the discussions of .live() and .unbind() for further details.

Note: In order for .die() to function correctly, the selector used with it must match exactly the selector initially used with .live().

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 () {
  $("#theone").live("click", aClick)
              .text("Can Click!");
});
$("#unbind").click(function () {
  $("#theone").die("click", aClick)
              .text("Does nothing...");
});

</script>

</body>
</html>

Demo:

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

$("p").die()

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

$("p").die( "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").live("click", foo); // ... now foo will be called when paragraphs are clicked ...

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

Support and Contributions

Need help with .die() 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 .die()? 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://www.google.com/profiles/wbednarski Wojciech Bednarski

    I’m trying to use die() inside live() method. It’s seems to be not working and I’m wondering why?

    $(‘selector’).live(‘click’, function() {
    $(this).die();
    // do stuff
    });

  • http://ehussain.in eHussain

    What is purpose of passing second parameter (function name) in .die() call?
    If suppose I will make call like
    $(“myButton”).live(“click” , myFunctionToBeCalledUponClick); //binds click event
    $(“myButton”).die(“click”); // don’t you think it should unbind click handler, whatever it is.
    so what is necessity of second parameter?

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

    Well, suppose you bind more than one live click handler to myButton? So now you have myFunctionToBeCalledUponClick and myOtherFunctionToBeCalledUponClick. The second parameter lets you kill just one of them.

  • Yader

    It isn’t working for me either. version 1.4.2

  • Tpneumat

    I have learned that .die() only works if you use a string selector and also it has to be the exact same string selector as you used with live. This means you have to do $('selector').die() in your demo above.

    Additionally, it is fairly dangerous to use .live() in a dynamic web application where you may navigate to various pages without a page refresh. Although this is really one of the reasons for .live in the first place, you can run into double-tripple-quadruple-etc binding your element with live fairly quickly unless your carefully kill your live events as you nav away or before rebinding. This can be done with namespaces and by using $(document).unbind('.mynamespace') rather than trying to use die() to kill off live event bindings.

  • mot

    If it is a 'feature' than why it is not mentioned in docs?
    I spent 2 hours before I understood that my script is not working coz of die() behaviour.

  • Micael

    Is it possible to reattach the detatched events? What I want to to is a general “disable” which detaches any binds and lives attached to the element, so that I later can “enable” it, i.e. reattaches the dead events.

  • Paul

    Tpneumat, Is this a bug? I am finding out that to use die() you are exactly correct – has to be a string selector – very inconvenient, and odd that it doesn't follow with other jquery type selecting.

    I am interested your namespace idea but know little about namespaces at this point. I take it you are suggesting to live bind things of a certain relation into a namespace, then when done with that group of things, to somehow destroy that namespace?

    That sounds clean.

  • Paul

    Tpneumat, Is this a bug? I am finding out that to use die() you are exactly correct – has to be a string selector – very inconvenient, and odd that it doesn't follow with other jquery type selecting.

    I am interested your namespace idea but know little about namespaces at this point. I take it you are suggesting to live bind things of a certain relation into a namespace, then when done with that group of things, to somehow destroy that namespace?

    That sounds clean.