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


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>

Updated on: 17-Feb-2020

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements