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 Listening for Events with addEventListener()

Only the first li responds during mouseover and mouseout. The newly created <li> elements do not respond. Please help!

This is my Javascript File:

const addGameInput = document.querySelector('input.addGameInput'); const addGameButton = document.querySelector('button.addGameButton'); const deleteGameButton = document.querySelector('button.deleteGameButton'); const listItems = document.getElementsByTagName('li');

for (let i = 0; i < listItems.length; i++ ) { listItems[i].addEventListener('mouseover', () => { listItems[i].textContent = listItems[i].textContent.toUpperCase(); });

listItems[i].addEventListener('mouseout', () => { listItems[i].textContent = listItems[i].textContent.toLowerCase(); }); }

addGameButton.addEventListener('click', () => { let ul = document.querySelector('ul') let li = document.createElement('li') li.textContent = addGameInput.value; ul.appendChild(li); addGameInput.value = ''; });

deleteGameButton.addEventListener('click', () => { let ul = document.querySelector('ul') let li = document.querySelector('li:last-child') ul.removeChild(li);

This is my HTML file:

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Home</title> <link rel='stylesheet' href='style.css'> </head> <body> <h1>Games</h1> <h2>Gameslist</h2> <ul> <li>One</li> </ul> <input type='text' class='addGameInput'> <button type='text' class='addGameButton'>Add</button> <button type='text' class='deleteGameButton'>Delete</button> <script src="script.js"></script> </body> </html>

1 Answer

Hi xajx In order to see the effect on every li. You can target the direct global element of li which is ul. And add the event to let the listener know which particular li to target.

Like this:

let ul = document.querySelector('ul');

ul.addEventListener('mouseover', (e) => { 
      if(e.target.tagName === 'LI'){
             e.target.textContent = e.target.textContent.toUpperCase();

ul.addEventListener('mouseout', (e) => { 
      if(e.target.tagName === 'LI'){
            e.target.textContent = e.target.textContent.toLowerCase(); 

Also, it's a good practice to wrap your code in 3 backticks. Like the way I did it. Otherwise, it's tough to understand the code.