How to override jQuery event handlers?

jQueryWeb DevelopmentFront End Technology

Use the off() method to override jQuery event handlers. This method is used to remove an event handler. The on() method is used to attach one or more event handler.

Example

You can try to run the following code to learn how to override jQuery event handlers −

Live Demo

<html>

   <head>
      <title>jQuery off() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {

            function aClick() {
               $("div").show().fadeOut("slow");
            }
               
            $("#bind").click(function () {
               $("#theone").on("click", aClick).text("Can Click!");
            });
               
            $("#unbind").click(function () {
               $("#theone").off("click", aClick).text("Does nothing...");
            });
               
         });
      </script>
       
      <style>
         button {
             margin:5px;
         }
         button#theone {
             color:red;
             background:yellow;
         }
      </style>
   </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>
       
   </body>
   
</html>
raja
Published on 20-Dec-2017 12:59:53
Advertisements