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 trialKent Hefley
11,217 PointsEvent Bubbling?
Andrew, in the video "Using the Same Callback on Multiple Elements," would it not have been better to utilize event bubbling and put the handler on the parent? In this case it would be the form.
4 Answers
Bruno Navarrete
Full Stack JavaScript Techdegree Graduate 22,246 PointsYou're right in thinking that event bubbling would trigger an event throughout every child and up to the parent. In this case though, we're trying to add classes to the individual input/textarea elements while they're selected (making the user experience a lot more reponsive).
Dora Tokai
16,003 PointsEvent delegating is a bit tricky when it comes to 'focus' and 'blur'. You need to register them in the capturing phase:
const myForm = document.getElementById('myForm'); // I added id="myForm" to the HTML form.
const nameInput = document.getElementById('name');
const messageTextArea = document.getElementById('message');
const focusHandler = (event) => {
event.target.className = 'highlight';
};
const blurHandler = (event) => {
event.target.className = '';
};
myForm.addEventListener('focus', focusHandler, true);
myForm.addEventListener('blur', blurHandler, true);
You'll find some useful information here: Delegating the focus and blur events
Luis Giraldo
Full Stack JavaScript Techdegree Graduate 27,531 PointsThank you Dora Tokai for this, I wasn't understanding why focus event wasn't firing.
Nghia Nguyen
Front End Web Development Techdegree Graduate 15,522 PointsFor those who prefer to use event bubbling by adding the listener on the form element instead, use thed focusin and focusout event type. The focus and blur event type does not bubble(read more on mdn).
Jason Dsouza
1,378 PointsHey all, i was wondering whether you could explain what Event Bubbling means??
Alan McClenaghan
Full Stack JavaScript Techdegree Graduate 56,501 PointsGuil Hernandez covers this in Javascript and the DOM. Great course, well worth taking it if you haven't already: https://teamtreehouse.com/library/event-bubbling-and-delegation
Kent Hefley
11,217 PointsKent Hefley
11,217 PointsI see. That makes sense. Thank you.