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.

.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 to be no longer 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.

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.min.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.

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .die() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • 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?
  • 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.
  • 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
    });
  • 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.
Time to generate: 2.07375