jQuery bind event


 

jQuery bind event

In this tutorial, we will discuss about how to bind multiple events with an element using jQuery.

In this tutorial, we will discuss about how to bind multiple events with an element using jQuery.

jQuery bind event

In this tutorial, we will discuss about how to bind multiple events with an element using jQuery. In this example, a paragraph binds with multiple events like click, mouseenter mouseleave,  dblclick etc using 'bind' function. When any of the event occurs , the action associated with that event will fire. When mouse enter into it's area , it will change it's color and if we click inside it's area, it will display coordinates where it clicked. And also , if we double click inside it's area it will show a message.

bindEvent.html

<!DOCTYPE html>
<html>
<head>
<style>
p { background:#DAA520; font-weight:bold; cursor:pointer;
padding:5px;width:20%;}
p.over { background: #ADFF2F; }
span { color:red; }
</style>
<script src="jquery-1.4.2.js"></script>
</head>
<body>
<p>Click or double click here.</p>
<span></span>
<script>
$("p").bind("click", function(event){
var str = "( " + event.pageX + ", " + event.pageY + " )";
$("span").text("Click happened! " + str);
});
$("p").bind("dblclick", function(){
$("span").text("Double-click happened in " + this.nodeName);
});
$("p").bind("mouseenter mouseleave", function(event){
$(this).toggleClass("over");
});

</script>
</body>
</html>

OUTPUT

When we click inside it's area :

When we double click inside it's area :

Download Source Code

Click here to see demo

Ads