Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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,638 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,314 Points

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

Steven Parker
Steven Parker
231,236 Points

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

Adam Beer
Adam Beer
11,314 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
231,236 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.