How would you unbind all jQuery events from a particular namespace?

jQueryWeb DevelopmentFront End Technology

To unbind jQuery events from a particular namespace, use the unbind() method. The event.namespace property is used to return the custom namespace when the event was triggered.

Example

You can try to run the following code to learn how event namespace works and how you can unbind the jQuery events from a namespace −

Live Demo

<!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("custom.myNamespace",function(event){
        alert(event.namespace);
    });
    $("p").click(function(event){
        $(this).trigger("custom.myNamespace");
    });  
    $("button").click(function(){
        $("p").off("custom.myNamespace");
    });
});  
</script>
</head>
<body>

<p>Click me</p>

<button>Click the button to remove namespace.</button>
<p>Clicking the above button removes the namespace.</p>

</body>
</html>
raja
Published on 21-Dec-2017 12:21:34
Advertisements