Create custom event handler, trigger custom event - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:on

Description

Create custom event handler, trigger custom event

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("myOwnEvent", function(event, showName){
        $(this).text(showName + " is me!").show();
    });/* ww w  . j  av a 2 s  .c o  m*/
    $("button").click(function(){
        $("p").trigger("myOwnEvent", ["java2s.com"]);
    });
});
</script>
</head>
<body>

<button>Trigger custom event</button>

<p>Click the button to attach a customized event on this p element.</p>

</body>
</html>

Related Tutorials