Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Responding to User Interaction The Event Object

In 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
Tim Strand
22,346 Points

open your console (F12) most likely you will see a message for unmatched <header> since you didnt close your header

Fixed 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(); }); }