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()

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,625 Points

Listening for Events with addEventListener()

I tried this , but it didn't work . Why? -

const toggleList = document.getElementById('toggleList');
const listDiv = document.querySelector('.list');
const descriptionInput = document.querySelector('input.description');
const descriptionP = document.querySelector('p.description');
const descriptionButton = document.querySelector('button.description');
const addItemInput = document.querySelector('input.addItemInput');
const addItemButton = document.querySelector('button.addItemButton');
const removeItemButton = document.querySelector('button.removeItemButton');
const listItems = document.querySelectorAll();

const toUpperCase = event => {
  event.target.textContent.toUpperCase();
}

const toLowerCase = event => {
  event.target.textContent.toLowerCase();
}

listItems.addEventListener( 'mouseOver' , toUpperCase);
listItems.addEventListener( 'mouseOut' , toLowerCase);
//here

toggleList.addEventListener('click', () => {
  if (listDiv.style.display == 'none') {
    toggleList.textContent = 'Hide list';
    listDiv.style.display = 'block';
  } else {
    toggleList.textContent = 'Show list';                        
    listDiv.style.display = 'none';
  }                         
});

descriptionButton.addEventListener('click', () => {
  descriptionP.innerHTML = descriptionInput.value + ':';
  descriptionInput.value = '';
});

addItemButton.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.createElement('li');
  li.textContent = addItemInput.value;
  ul.appendChild(li);
  addItemInput.value = '';
});

removeItemButton.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let li = document.querySelector('li:last-child');
  ul.removeChild(li);
});

2 Answers

Adam Beer
Adam Beer
11,313 Points

Please change your last "querySelectorAll()" to "getElementByTagName()". And correct your code. Please check again the video.

Steven Parker
Steven Parker
216,175 Points

You probably meant "getElement‍s‍ByTagName" (with an "s"), and neither one will work correctly without an argument.

Adam Beer
Adam Beer
11,313 Points

I did not write down the complete solution. After that I saw that the code was bad several places at the beginning

Steven Parker
Steven Parker
216,175 Points

The call to "querySelectorAll" requires a selector string argument to operate correctly.

Also, the "toUpperCase" and "toLowerCase" methods don't alter the strings they are applied to, they just return a new string with the changes. For this to be useful in a program, the returned value would need to be assigned to a variable or passed as an argument to a function.

For future questions, to facilitate a complete analysis of complex project issues, make a snapshot of your workspace and post the link to it here.