Event Handling versus Event Delegation

If you ever wondered how flexible web applications with hundreds of objects that need event handling are built, the trick behind it is called “Event Delegation”.

I didn’t come up with it, and it is nothing new either, but as some people seem to get confused when the term crops up, I’ve written a quick example why event delegation helps keeping web apps light and fast.

9 Responses to “Event Handling versus Event Delegation”

  1. Joshue O Connor Says:

    Interesting article Christian but the adding another item to the Handler list worked fine when I clicked it. (Firefox, Mac OS 10.4)

    Josh

  2. André Huf Says:

    ppk not only knows, but writes extensively about this in his much recommended book

  3. Valentin Cekov Says:

    Not all people know about the YAHOO JS library innards to understand your point. A simple HTML + a regular javascript illustrating the mechanism would have sufficed a lot. I doubt people with little knowledge of DOM and JS would understand what you meant.

  4. Ariel Says:

    nicely put. good idea, i like it. will try it out right away.

  5. Anthony Ettinger Says:

    I understand the reasoning if you want all your child elements to do the event, but what should I do in the case of a behavior like:

    ul.onmouseout=closeAllChildren;

    The problem I’m having is that all the children elements will also call the function when I leave them, but am still inside the UL element.

    Please help, this has been escaping me for awhile.

  6. Chris Heilmann Says:

    Anthony this is as easy, just add another handler to the UL elements inside the others or handle the functionality in the listener method. The listener method gets everything in the element, so you can compare the nodeName of the target element with UL and do other stuff.

  7. Dan Says:

    How do you go about using either (bubbling or handling) to the history plugin by Klaus?

    $(’a.remote’).remote(’#place’, function() {
    if (window.console && window.console.info) {
    console.info(’content loaded’);
    }

    });

    $.ajaxHistory.initialize();

  8. Diego Perini Says:

    Christian,
    this is the most well written description of Event Delegation, my congrats, info on this is scarce.

    I used it so much that I should make my own scripts to handle that with selectors. I can actually handle also the “focus” and “blur” events cross-browser by implementing a small trick in the handling/fixing of events.

    You can see my work on delegates here:

    http://javascript.nwbox.com/NWEvents/

    Look under Delegates to see the only example specific to this method bound to CSS3 Selectors.
    Though my syntax is not that short compared to jQuery or similar, I think the idea is unique.

    I was searching for “delegates” related to “onload” problems but was not able to find anywhere talking about the fact that “Event Delegation” can lead to avoid having to resort to “onload” or similar method to attach functionality to the page at initial startup.

    You seem to have a deep knowledge of these techniques, I would like your suggestion and comments on my implementation and possible improvements.

    Do you mind if I copy (with credits) some Delegation example to include on my site ?

  9. Steve Bush Says:

    This should be called “The benefits of using Event Bubbling” and not have a new obscure term Event Delegation invented for it.

Leave a Reply

@mediaAjax
Scripting Enabled - hacking the web to be more accessible - London, England 19th and 20th of September 2008
Wait till I come! is the blog of , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).