Simultaneously add Multiple Event Handlers to a Selector using jQuery 1.4

With jQuery 1.4, you can add multiple event handlers to a selector by passing a map of event type/handler pairs as shown below:

<html>
<
head>
<
title>Bind Multiple Event Handlers</title>
<
style type="text/css">
.divOne{
height:40px;
width:100px;
background-color:#808080;
}
</style>
<
script language="javascript" type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js">
</
script>
<
script language="javascript" type="text/javascript">
$(function() {
$('.divOne').bind({
mouseenter: function() {
$(this).css("background-color", "#f0f0f0");
},
mouseout: function() {
$(this).css("background-color", "#808080");
},
click: function() {
alert("Div was clicked");
}
});
});
</script>
</
head>
<
body>
<
form>
<
div class="divOne">
</
div>
</
form>
</
body>
</
html>

See a Live Demo

Note: In the previous version, you could bind one function to multiple events.



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

night-fairy-tales.com said...

Interesting possibility. Thanks