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 trialPhuc Nguyen
9,958 PointsJavascript and the DOM - Appending Nodes
Hi
I followed the instructions but still the last part didn't pass.
addItemButton.addEventListener('click', () => { let ul = document.getElementsByTagName('ul')[0]; let li = document.createElement('li'); li.textContent = addItemInput.value; ul.appendChild(li); });
///////
What was the error? I looked at the console and it said "Uncaught TypeError: Cannot read property 'addEventListener' of null"
Thanks
1 Answer
Stephan Olsen
6,650 PointsYou need to declare the addItemButton before you add an event listener to it. I don't know excactly which challenge you are doing, but it should look something like this.
const addItemButton = document.getElementsByClassname('classnameOfButton');
addItemButton.addEventListener('click', () => {
let ul = document.getElementsByTagName('ul')[0];
let li = document.createElement('li');
li.textContent = addItemInput.value;
ul.appendChild(li);
});
Phuc Nguyen
9,958 PointsPhuc Nguyen
9,958 PointsThanks Stephan :-)