SIGN UP MEMBER LOGIN:    
ARTICLE

JQuery Event Propagation

Posted by Sateesh Arveti Articles | JQuery March 19, 2010
In previous article, we looked into events present in JQuery. In this article, we will cover JQuery event propagation followed by firing events like button click through code.
Reader Level:

In previous article, we looked into events present in JQuery. In this article, we will cover JQuery event propagation followed by firing events like button click through code. JQuery supports event bubbling. It means, when an event is fired on a child element. It will get propagate to its parents for handling by them as shown below:
 
In below HTML, when you click the button, it will fire click event of button as well as div element.
 
<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').click(function(){alert('Button clicked');});
$('#myDiv').click(function(){alert('div clicked');});
});
</script>
  </head>
  <body>
<div id='myDiv'>
<input type="button" value="My Button" id="myButton"/>
</div>
  </body>
</html> 

We are not interested in firing click event of parent element [div]. We can rewrite above code to avoid event propagation as shown below:
 
$(document).ready(function() {
$('#myButton').click(function(){alert('Button clicked');});
$('#myDiv').click(function(event){if(event.target==this)alert('div clicked');});
});
 
We can check for event's target property before executing the code to avoid propagation. The other way to stop event propagation using event.stopPropagation() is shown below:
 
$(document).ready(function() {
$('#myButton').click(function(event){alert('Button clicked');event.stopPropagation();});
$('#myDiv').click(function(){alert('div clicked');});
}); 
 
Now, we will look into firing an event through code. In certain cases, we might need to fire button click event through code. This can be done using below code:

$('#myButton').click();// Executes code associated with myButton click event.


(or)
 
$('#myButton').trigger('click');
 
Tip:  We can write $(document).ready(function() {}) in shortcut as shown below:
 
$().ready(function()  {
.........
});
(or)
$(function() {
.........
});
 
I am ending the things here. In next article, we will discuss about Effects available in JQuery. I hope this article will be helpful for all.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • 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. Visit DynamicPDF here
    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.
Team Foundation Server Hosting
Become a Sponsor