Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » JQuery » Events in JQuery

Events in JQuery

In previous article, we looked into traversal methods and chaining. In this article, we will cover events and its types.

Author Rank :
Page Views : 3705
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


In previous article, we looked into traversal methods and chaining. In this article, we will cover events and its types. Earlier, we looked into page load event by using $(document).ready(). There are lot more events available on DOM elements like button, textbox etc.  For example, we might need to execute a piece of code on click of button or key press in textbox. JQuery event handling mechanism provides elegant methods to make a page dynamic and responsive. Let's start with a sample button click event. We can use bind() method for binding the click event of a button with JQuery code to be executed as shown below:

<html>
  <head>
    <title>JQuery Sample HTML Page</title>
<script src="jquery-1.4.2.min.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#myButton').bind('click',function(){$(this).css('color','red');}); // this refers to myButton.
});
$('#myButton').bind('mouseenter',function(){$(this).css('color','red');});
$('#myButton').bind('mouseleave',function(){$(this).css('color','black');});
</script>
  </head>
  <body>
<input type="button" value="My Button" id="myButton"/>
  </body>
</html> 

bind() accepts two parameters. First parameter is event type and next one is the JQuery code to be executed on event firing. In similar way, we can unbind an event by using unbind() method as shown below for button click.

$('#myButton').unbind();// Unbinds all events of myButton.
$('#myButton').unbind('mouseleave'); // Unbinds mouseleave event only.
$('#myButton').unbind('mouseleave',fn); // Unbinds fn function for mouseleave event.

Few other event types are: mouseenter, mouseleave, dblclick etc. JQuery provides a shortcut to bind an event handler. We can bind mouse events to the button using below code in shorthand:

$('#myButton').mouseenter(function(){$(this).css('color','red');});
$('#myButton').mouseleave(function(){$(this).css('color','black');});

Up to now, we looked into events supported by JavaScript internally. JQuery supports custom events toggle() and  hover(). toggle() method fires specific function based on number of clicks as shown below:

$('#myButton').toggle(function(){alert('1st click');},function(){alert('2nd click');},function(){alert('3rd click');});

On first click, it fires first function and second one on second click, and so on. 

hover() provides hovering on a element. It takes two parameters. First one is the function to be executed on mouse over and second one on mouse off the element as shown below:

$('#myButton').hover(function(){$(this).css('color','red');},function(){$(this).css('color','blue');});

The above code changes color of button to red on mouse over and to blue on mouse off. 

We will end up this session by looking into live() method. live() is similar to bind(), but it will cause the event to be bind to all present and future matched elements. 

<html>
  <head>
    <title>JQuery Sample HTML Page</title>
<script src="jquery-1.4.2.min.js" type="text/javascript">
</script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('input').live('click',function(){alert('clicked');});
$('#myDiv').html('<input type="button" value="My Button" id="myButton"/><input type="button" value="My Button1" id="myButton1"/>');
});
</script>
  </head>
  <body>
<div id='myDiv'>
<input type="button" value="My Button" id="myButton"/>
</div>
  </body>
</html>

By using live(), we can make sure the click event handler will be added to all buttons created later [based on matching criteria].We can refer http://docs.jquery.com/Events for complete list of events supported by JQuery.

I am ending the things here. I hope this article will be helpful for all.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Sateesh Arveti
I hold Bachelors degree in Computer Science along with MCSD,MCTS and MVP for the year 2009. Areas of Interest: C#, WPF, WF, silverlight, ASP.NET, Oracle and SQL Server.

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.