Ah-ha Event Delegation

5 March 2008

Wrapping my head around things like OOP took months. It’s not because I’m an idiot (I don’t think) - it’s just because I needed all the explanations to marinate before having an ah-ha! moment.

I had an ah-ha today, with regards to Event Delegation. I’ve never seemed to completely understand events, yet I use them all the time. They’re an essential aspect of Actionscript and Javascript and there are two basic ways of capturing events, Event Handling and Event Delegation.

Most of us, whether we realize it or not, are familiar with Event Handling. Imagine we have an unordered list with a class name .nav and each item has a link. For this example, I’m going to use jQuery, so if you wanted to call a javascript function when the user clicks a link in the list, you’d do the following:

$('.nav a').each(function() {
    $(this).click(handleClick);
});

function handleClick(event) {
    // fun stuff goes here
}

If you’re jQuery savvy, the above code should be familiar. We’re looping over all the anchor tags within the unordered list and assigning the function “handleClick” to the click event. Each time someone clicks a link, “handleClick” is called and the anchor tag is in the function scope. This is Event Handling.

Event Delegation makes things a little simpler by eliminating the need to loop over every anchor tag. We simply apply the click event to the unordered list like so:

$('.nav').click(handleClick);

Since the click event is registered with the unordered list, it gets fired off when anything inside the list is clicked. The scope of the handleClick function is set to the <ul> but if you want to target the anchor tag below the mouse click, you use event.target. This is Event Delegation.

Both are very powerful but in most instances Event Delegation is going to require less code while boosting performance. Many thanks to Jacob for making this ah-ha moment possible.

Related tags: delegation, events, javascript, jquery

Remarks

Simon Scarfe http://breakfastdinnertea.co.uk

Christian Heilmann is a big advocate of Event Delegation, his example at http://icant.co.uk/sandbox/eventdelegation/ involves putting a global handler on the body element (because everything eventually bubbles there anyway), checking the target, and reacting accordingly.

While I like it for its loopless elegance, I can see the potential for a huge mess accumulating with lots of event types / targets.

kevin http://www.scribesonic.com

good pick on the jquery tip.

normally I’d have just selected the anchor tags in my jquery selector statement using this selector syntax…

$(‘.nav a’).click(handleClick);

which would select [ancestor] [descendant] but I can def see where you’d want to make use of event.target in other situations.

docs: http://docs.jquery.com/Selectors/descendant#ancestordesce…

or you could use parent > child: http://docs.jquery.com/Selectors/child#parentchild

gotta love jquery!

Nathan Borror http://www.playgroundblues.com

@kevin - Agreed. The selector syntax is one of the biggest reasons I use jQuery.

Remarks are closed.

Remarks have been close for this post.