the Future of the Web
  • Home
  • Hire Us
  • Articles
  • About
  • Contact
  • Bursting the Event Bubble

    Feb 15 2006

    Have you ever wanted to prevent a JavaScript event from firing when it has been bubbled up from a child element? For example, let's say you have an onMouseOver event for a parent element. Anytime the mouse moves over any of the children elements, the parent's onMouseOver event will keep firing. Or, let's say you have an onMouseDown event on a parent and another on its child. Both onMouseDown events will fire when someone clicks the child. Sometimes this can be a real pain.

    Anyway, enough examples, let's look at a way to avoid this. We need a way to fire an event only when the element with the event handler is the target element of the event. Here's the solution event handler:

    function eventHandler(e) {
         e = e || window.event;
         var target = e.srcElement || e.target;
         if (target != this) return;
         
         // the rest of the function
    }

    Simple enough, once we have the code. Let's look at what it does.

    First, we find the event object. In Firefox and Safari, it's a parameter to the function. In Internet Explorer, it's in window.event.

    Having that, we need to find the target in the event object. This is the first element the event fires on, the inner-most or top-most element. In Safari and Firefox, it's target, but in Internet Explorer it's srcElement.

    Finally, we will compare against the element the event is attached to. The element the event handler is attached to can be accessed easily with the this variable.

    For more information on event handlers, I highly recommend the excellent resources at QuirksMode:

    • Events order (event bubbling vs. capturing)
    • Event Properties
    • Event compatibility tables

    Update: you need to do a little bit more if you are dealing with mouseout events. If you move the mouse into a child, this triggers the mouseout event on the parent. In this case, the target will match the parent. So, you'll also have to get the 'related' element (the child) and make sure the target isn't one of the ancestors. Here is the updated function for mouseout events:

    function mouseOutEventHandler(e) {
         e = e || window.event;
         var target = e.srcElement || e.target;
         if (target != this) return;
    
         var related = e.relatedTarget || e.toElement;
         while (related != this && related.nodeName != 'BODY')
              related = related.parentNode;
         if (related == this) return;
       
         // the rest of the function
    }

    Special thanks to QuirksMode's Mouse Events page for helping me figure out that one!

    Tags: javascript eventhandler scripting cross-browser quirksmode
    View 3 Comments | Add a comment
  • Comments

    1. Steven Stoft at 9:44am on January 29, 2007

    I'm currently using onmouseover(param1, p2) inside a div tag, but I would like to use your code to solve the bubbling problem.
    However I cannot see how to pass my parameter to your mouseOutEventHandler(e) so that the "rest of function" can do what I need.

    Nice web site. Thanks.
    Steve

    2. Diego at 9:51am on November 8, 2007

    I can't seem to burst the event bubble, I've spent countless hours on my specific scenario without avail. Please help.

    I tried the code above but I get a strange result. The code below 'the rest of the function' only fires when the mouse leaves the right side of the div.

    I have a div with other elements in it, I need to fire the mouseover/mouseout events only when I leave the whole div that contains everything else.


    Regards,


    Diego

    3. Llewellyn at 9:47am on January 28, 2008

    Here's another easy way to stop the event from bubbling from the event handler:

    function doSomething(e)
    {
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
    }

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Request a Quote

  • Jesse Skinner

    Jesse Skinner
    • Hire Me
    • About Me
    • Email Me
    • RSS Feed RSS Icon
  • Recent Articles

    • Google is Hosting Ajax Libraries
    • Parse Accept-Language to detect a user's language
    • Twitter
    • Three years of The Future of the Web
    • Saving data to a file with PHP
    • Easy web scraping with PHP
    • See all the articles
    • IBM: Where and when to use Ajax
    • Code Igniter 1.6.0 Released
    • Update a Dev Site Automatically with Subversion
    • See All...
  • Categories

    • javascript (37)
    • links (20)
    • about (19)
    • web (14)
    • server (10)
    • html (10)
    • css (8)
    • carnival (7)
    • browsers (7)
    • design (4)
    • seo (4)
    • ads (4)
    • standards (4)
    • events (4)
    • work (4)
  • Older Articles

    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2008 Jesse Skinner | CSS | XHTML | RSS | Help | Impressum | Cutie Quilts | Internet Blog Top Sites