Preventing Default Actions In A Bubbled Event In jQuery

Posted June 12, 2009 at 3:07 PM

Tags: Javascript / DHTML

I was just reading over on Jon Hartmann's blog about leveraging event bubbling in jQuery. It's a very cool concept and one that I have not played around with enough so far. When I was reading his post, however, I immediately wondered if default actions could be prevented using this technique. For example, if I have links within a list and the click handler is on the list (UL), can I prevent the default action of the link (navigate away from page) once the event has bubbled up beyond the link? Or, is it too late at that point? To test this, I put together a little demo in which the ancestor element, the UL, prevents the default on all actions:

 
 
 
 
 
 
 
 
 
 

As you can see from the video, even though the UL is handling an event that has bubbled past the link (target element), it is still able to prevent the default action of the link navigation. I'm sure if I had a richer understanding of the browser's event model, this would be obvious; but, I'm still learning. If I had to extrapolate this out into a meaning, it seems that you have the ability to control an event outcome until the browser has stopped handling it at every different level of listening.

Here is the code that I executed in the demo:

 Launch code in new window » Download code as text file »

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Event Bubbling In jQuery</title>
  • <script type="text/javascript" src="jquery-1.3.2.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready to be interacted with,
  • $(function(){
  •  
  • // Bind click event handling on the UL. This will allow
  • // us to handle both link and LI clicks in one place
  • // by leveraging event bubbling.
  • $( "ul" ).click(
  • function( objEvent ){
  •  
  • // Return FALSE to prevent the default.
  • return( false );
  •  
  • }
  • );
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • Event Bubbling In jQuery
  • </h1>
  •  
  • <ul>
  • <li>
  • Go to <a href="http://www.t-nation.com">T-Nation</a>
  • </li>
  • <li>
  • Go to <a href="http://www.cfbloggers.com">CF Bloggers</a>
  • </li>
  • <li>
  • Go to <a href="http://www.nycfug.com">NY CFUG</a>
  • </li>
  • </ul>
  •  
  • </body>
  • </html>

I am not sure that I know what kind of situation this is best suited for; but, I think it's worth playing around with some.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jun 12, 2009 at 6:25 PM // reply »
31 Comments

I've been using this technique for a while now.
In one example I have a link that has different functions when JavaScript is disabled or not.
With JavaScript is disabled, the link opens a new page with a form.
With JQuery (JavaScript Enabled), the link opens a JQuery UI Dialog, and loads the form via the JQuery load() event. I use return false to prevent the browser from navigating to the page with the form.

Very useful!


Jun 12, 2009 at 6:53 PM // reply »
7,538 Comments

@Brian,

Sounds like cool stuff. I love jQuery!


Jun 13, 2009 at 10:29 AM // reply »
1 Comments

I have had the same problem with Java, thanks for the information. I am pretty new at this and all the help I can get is way appreciated. Thanks again. Spence


Jun 13, 2009 at 10:36 AM // reply »
15 Comments

You're right about the fact that it is due to the browser's event model.


Jun 13, 2009 at 6:38 PM // reply »
10 Comments

I usually target anchors specifically with a ev.preventDefault() and override to whatever behavior I wish.

I've not tested this, but judging by the online documentation ... if I were to do preventDefault() on my anchors and bind some event to the ul, jQuery would still bubble up and I could even fire the default behavior that I initially prevented on the anchor. This could be a new way to identify anchor events by their parents rather than looking up the information directly from the anchor. Pretty cool if it works that way


Jun 15, 2009 at 8:37 AM // reply »
7,538 Comments

@Drew,

Right, I believe you'd have to get the reference to the target first, before you could look up any info. Example:

$( objEvent.target ).attr( "href" )


Jun 15, 2009 at 11:10 AM // reply »
1 Comments

I think the example above its pretty good, I think another key point is to mention is garbage collection and memory management, if you were to create a listener for lets say two hundred objects that doesn't seem so bad, but just wait when the page crawls to a halt in ie, its better to listen to an object such as a parent div, then find out which child element fired off the event, the performance gains are huge. Now if you were to use prototypejs instead of jquery, you can create custom events, as well it has support for binding, getting around the problem of scoping with the keyword this, its particuallry handy when using a method as event listener rather than a function as shown in the example above.


Jun 15, 2009 at 11:26 AM // reply »
31 Comments

@James: How's the performance of using the JQuery live() event binding method?


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 18, 2010 at 6:03 AM
ColdFusion Session Management Revisited... User vs. Spider III
Hi Ben, no, the timour is not dynamic at all. The two code chunks are: // This application definition is for robots that do NOT need sessions. this.name = "eClipse_KABS_Boomerang" ... read »
Mar 18, 2010 at 1:25 AM
Ask Ben: Blocking WSDL Access In A ColdFusion Application
Well,I have to say,this is too complicated for me to understand.But I think you're great,keep doing it. ... read »
Mar 17, 2010 at 11:36 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
@Ben, It is. But I have tested again and sleep() seem to be keeping windows hanging. As a result, multiple people were having issues uploading and removing images when they are trying to do the imag ... read »
Mar 17, 2010 at 11:22 PM
Google Maps Not Working in Internet Explorer (IE)
@James The page has a problem in IE7, because line 112 has a comma at the end. Line 112 is the first problem, there are many more with the same. Ralph ... read »
Mar 17, 2010 at 8:39 PM
Looping Over ColdFusion JSON Queries In jQuery
Thanks Ben - this is just what I needed - just getting up to speed with jQuery - and your posts are accelerating that process substantially :) ... read »
Mar 17, 2010 at 7:50 PM
ColdFusion ArraySplice() Method As An Example Of A Dynamic Method Signature
Java is awesome and I love seeing new twists on it. @Ben - I totally agree with you. ... read »
Mar 17, 2010 at 4:13 PM
Testing For NULL Values In A ColdFusion Query Result Set
To get around the empty strings in an UPDATE statement I pretended the variable was a string by enclosing it in single quotes, CAST it to an integer, let it convert empty strings to zeroes, and used ... read »
Mar 17, 2010 at 4:12 PM
Ask Ben: Environment-Based Application.cfc Settings
Ben, those are valid comments. I posted some code that shows how to "worryfree ;o)" copy code and let the Application.cfc sort it out. http://boncode.blogspot.com/2010/03/cf-dynamically-changing-ap ... read »