jQuery off() Method

jQueryWeb DevelopmentFront End Technology

The off() method in jQuery is used to remove event handlers attached with the on() method.

Syntax

The syntax is as follows −

$(selector).off(event,selector,function(eventOb),map)

Above, the event parameter specifies one or more events or namespaces to remove from the selected element(s), function(eventOb) is the function to run when the event occurs. The map parameter is the event map.

Example

Let us now see an example to implement the jQuery off() method 

 Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("p").on("click", function(){
         $(this).css("font-size", "1.5em");
      });
      $("button").click(function(){
         $("p").off("click");
      });
   });
</script>
</head>
<body>
<h2>Demo Heading</h2>
<p>Click here to change the font size</p>
<button>Remove</button>
</body>
</html>

Output

This will produce the following output −

Click on the text to change the font size −

Now, run the script again. Click “Remove” to remove the event handler. After removing, when you will click on “Change”, then the font size won’t change −

raja
Published on 30-Dec-2019 09:36:58
Advertisements