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

Isaac Hughes
Isaac Hughes
3,711 Points

I do not know what is happening with the code, it is not working

I looked over the code and checked it a bunch of times but i cannot get it to work the error i get in the console is "SCRIPT438: SCRIPT438: Object doesn't support property or method 'addEventListener' "

https://w.trhou.se/kp22mj0764

const toggleList= document.getElementById('toggleList');
const listDiv= document.getElementsByClassName('list');
const input=document.querySelector('input');
const p=document.querySelector('p.description');



listDiv.addEventListener('mouseover',(event) =>{
  if(event.target.tagName =='LI') {
    event.target.textContent =  event.target.textContent.toUpperCase();
  }
});

listDiv.addEventListener('mouseout',(event) => {
  if(event.target.tagName =='LI'){
   event.target.textContent =  event.target.textContent.toLowerCase();
  } 
});

1 Answer

I made the following change:

const listDiv= document.getElementsByClassName('list')[0]

note the index at the end. getElementsByClassName retrieves a collection of elements so the code above references the first item.

Isaac Hughes
Isaac Hughes
3,711 Points

can't believe I missed that, it works now thanks :)