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 trialAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsListening 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
11,314 PointsPlease change your last "querySelectorAll()" to "getElementByTagName()". And correct your code. Please check again the video.
Steven Parker
231,269 PointsThe 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.
Steven Parker
231,269 PointsSteven Parker
231,269 PointsYou probably meant "getElementβsβByTagName" (with an "s"), and neither one will work correctly without an argument.
Adam Beer
11,314 PointsAdam Beer
11,314 PointsI did not write down the complete solution. After that I saw that the code was bad several places at the beginning