off() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:off

Description

The off() method removes event handlers attached with the on() method.

Syntax

$(selector).off(event,selector,function(eventObj),map);
Parameter Require Description
event Required.one or more events or namespaces to remove from the selected element(s).
selector Optional. match the one originally passed to the on() method when attaching event handlers
function(eventObj) Optional. function to run when the event occurs
mapOptioanl. an event map ({event:function, event:function, ...}) to attach to the elements to run when the events occur

The following code shows how to Remove the click event for all <p> elements:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").on("click", function(){
        $(this).css("background-color", "pink");
    });//w ww.  jav a2s .  c om
    $("button").click(function(){
        $("p").off("click");
    });
});
</script>
</head>
<body>

<p>Click this paragraph to change its background color.</p>
<p>Click the button below and then click on this paragraph (the click event is removed).</p>

<button>Remove the click event handler</button>

</body>
</html>

Related Tutorials