JavaScript JavaScript and the DOM 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(); }); }