Home >>jQuery Tutorial >jQuery off() Method

jQuery off() Method

jQuery off() Method

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

Syntax:
$(selector).off(event,selector,function(eventObj),map)

Parameter Values

Parameter Description
event It is a required parameter and is used to specifies one or multiple events to remove from the selected elements
selector It is an optional parameter used as a selector which should match the one originally passed to the on() method
function(eventObj) It is an optional parameter and used to specifies the function to run when the event occurs
map It is used to specifies an event map containing one or multiple event to attach to the elements
Here is an Example of jQuery off() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".txt").on("click", function(){
$(this).css("background-color", "purple");
});
$(".btn1").click(function(){
$(".txt").off("click");
});
});
</script>
</head>
<body>
<p class="txt">Click me to change its background color.</p>
<p class="txt">Click the button below and then click on this paragraph (the click event is removed).</p>
<button class="btn1">click me to Remove the click event handler</button>
</body>
</html>

Output:

Click me to change its background color.

Click the button below and then click on this paragraph (the click event is removed).