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 trialNicolas Valdivieso
6,654 PointsI cant figure out why every single button is logging third :
2 Answers
Steven Parker
231,169 PointsIt works for me (I get each button's name).
But you don't need separate handlers for each button. This is a good situation to use event delegation:
document.body.addEventListener('click', function(e) {
if (e.target.tagName == "BUTTON")
console.log(e.target.innerHTML)
});
Nicolas Valdivieso
6,654 Pointsyeah i fixed it earlier while testing and i forgot... i broke it again so you can see the actual problem
Steven Parker
231,169 PointsSteven Parker
231,169 PointsNow that I've seen the original, the problem was caused by having one "buttonName" shared by all 3 handlers. So before any of them could be triggered, the value of "buttonName" was set to the text from the last one.
A quick fix would be to use "let" instead of "var" when creating the variable.