×

jQuery Tutorial

jQuery Examples

jQuery Practice

jQuery die() Method

jQuery | die() Method: Learn about the jQuery die() Method with its usages, syntax, and examples.
Submitted by Pratishtha Saxena, on October 22, 2022

die() Method

Events in jQuery are the actions that the user performs on the web page. It can be anything – related to mouse clicks, keyboard presses, etc. Using jQuery, we can control these events in the order we want and can also attach some custom functions to it if needed. That means, we can use predefined event methods for the actions and also define a function that gets fired when the event method is triggered. Overall, this makes the website more dynamic on the user's end. Let's learn about the die() method here.

The die event triggers the die() method in jQuery. This method helps us to remove the attached event handler from the selected elements. It only removes the event handlers that have been declared using the live() method of jQuery for the element.

die() Method Syntax

$('selector').die();
$('selector').die('event Name',function(){});

The event name that has to be removed can be specified, and if nothing is provided, it will remove all the event handlers from that element. A function can also be declared which gets fired when the event is triggered.

Note: Die event has been deprecated and then further removed from version 1.9. Therefore, now we use .off() method instead for the same.

The below example shows, the event gets fades in when clicked on it which has been declared using live(). Now, to remove this event, the die() method has been used on that selector.

jQuery die() Method Example

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <title>Document</title>
  </head>
  
  <body>
    <h2>jQuery Event - Die</h2>
    <p>Click the following Image to implement the die() method in jQuery.</p>
    <button>Die</button>
    <hr>
    <div>DIV One</div>
    <div>DIV Two</div>
    <hr>
    <h3></h3>
  </body>
  
  <script type="text/javascript">
    $(document).ready(function(){
        $('div').live('click',function(){
            $(this).css('color','crimson');
        });
    
        $('button').click(function(){
            $('div').die();
        })
    });  
  </script>
</html>



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.