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 trialDale Sublett
14,231 PointsIn this video, you'll learn how to add an event listener to a parent element and let it handle events on its children.
So I watched this video and followed along in my own text editor while also trying to use this same event listener in my personal project, but it isn't working for me. What am I doing wrong?
<div class="container-header nav-header navList">
<header class="main-header">
<ul class="nav">
<li><a href="#">About</a></li>
<li><a href="#">Repairs</a></li>
<li><a href="#">Volunteer</a></li>
</ul>
</div>
const listDiv = document.querySelector('.navList');
listDiv.addEventListener('mouseover', function (event) { if (event.target.tagName == 'LI') { event.target.textContent = event.target.textContent.toUpperCase(); } }); listDiv.addEventListener('mouseout', function (event) { if (event.target.tagName == 'LI') { event.target.textContent = event.target.textContent.toLowerCase(); } });
2 Answers
Tim Strand
22,458 Pointsopen your console (F12) most likely you will see a message for unmatched <header> since you didnt close your header
Dale Sublett
14,231 PointsFixed that, but it didn't work. I used Guil's previous code that did work, i just wanted to try it this other way as well, but haven't gotten the same results.
this one worked.... var navItems = document.getElementsByTagName('a');
for (let i = 0; i < navItems.length; i += 1) { navItems[i].addEventListener('mouseover', function() { navItems[i].textContent = navItems[i].textContent.toUpperCase(); }); navItems[i].addEventListener('mouseout', function() { navItems[i].textContent = navItems[i].textContent.toLowerCase(); }); }