Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Nancy Melucci
Courses Plus Student 36,159 PointsWhat exactly is "event delegation?"
I have a simple HTML form powered by JS that takes 4 membership levels and a number of years and produces a fee (with a multi-year discount.) There is a calculate button at the bottom. Right now, the user chooses the level and inputs the number and the form immediately produces the fee.
I've been instructed to adjust the event delegation so that the calculation "bubbles up" to the parent element.
What I am struggling with is this: If the fee is immediately appearing without event listener for the button being triggered, isn't the event already being delegated to the parent (the form)?
I guess I feel like I don't really understand event delegation. Shouldn't the "delegation" go in the opposite direction (shouldn't the parent delegate to the button.)
The instructions I have pretty clearly say "delegate to the parent."
I feel kinda dumb. Help.
Nancy
6 Answers

William Mackay
25,189 PointsA good example might be a ul, say that something needs to happen when each child element is clicked. You could add a separate event listener to each individual li element, but what if li elements are frequently added and removed from the list? Adding and removing event listeners would be annoying. The better solution is to add an event listener to the parent ul element. When the event bubbles up to the ul element, you check the event object's target property to gain a reference to the actual clicked node.
Event delegation allows you to avoid adding event listeners to specific nodes; instead, the event listener is added to one parent. That event listener analyzes events to find a match on child elements.

Steven Parker
242,296 PointsEvent delegation allows a parent element to handle events for its children. In JavaScript, event propagation (also called "bubbling") causes the parents of an element to receive the same events as the actual target element. This means you can attach an event handler to a parent, add some code to identify the actual target, and handle the event from the parent. There are three significant advantages of doing this:
- One handler can deal with events from any number of child elements.
- The handler can be established even before the element(s) it will respond to.
- Less memory will be used than having multiple handlers.
If you are using (or are allowed to use) jQuery, establishing delegated handlers is easily done using a variant of the .on() method which has an extra argument to identify the target(s) of the delegation.

Nancy Melucci
Courses Plus Student 36,159 PointsI believe I am allowed to use jQuery. It sounds like what I should do is remove the function calls for the individual nodes and replace with a jQuery function that will calculate the fee when any child element is clicked. Let me know if I understood that correctly. Thank you so much.

Steven Parker
242,296 PointsIf by "function calls" you mean "event handlers", then it sounds like you have the right idea. The new handler would be associated with a parent element common the each child that requires interaction. Did you review the documentation for .on() and are comfortable with setting it up?

Nancy Melucci
Courses Plus Student 36,159 Points.on() looks like JQuery. I don't have that much experience with it (I've written a little bit) and I had to put this task aside temporarily to work at my day job. I'll take a look at the JQuery API before I try to revise the program.
Thanks.
NJM

Steven Parker
242,296 PointsIt is indeed jQuery, that's why I asked if you were using (or could use) jQuery here. You don't need jQuery to establish delegated event handling, it just makes it easy.
There's an example of doing it without jQuery in the Event Bubbling and Delegation video as part of the JavaScript and the DOM course.

Nancy Melucci
Courses Plus Student 36,159 PointsThanks I will definitely look at this before I proceed with this. NJM

Nancy Melucci
Courses Plus Student 36,159 PointsI dug up a JS/JQuery book this AM and found some nice sample code for an event delegation. Between Treehouse and this, I was able to understand and add the functionality to my project.
Thank you. I am so grateful for the help from Treehouse.
Nancy M.