JavaScript JavaScript and the DOM Responding to User Interaction Listening for Events with addEventListener()

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,608 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,268 Points

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

Steven Parker
Steven Parker
172,140 Points

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

Adam Beer
Adam Beer
11,268 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
172,140 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.