How does jQuery event namespace works?

jQueryWeb DevelopmentFront End Technology

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 to create and remove 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>
<p>Click above to generate an alert box. Click the below button to remove namespace, which won’t generate an alert box.</p>
<button>Click this button to remove namespace.</button>

</body>
</html>
raja
Published on 20-Dec-2017 09:03:14
Advertisements