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 trialbaloch baloch
63 Pointswhat's wrong with my code it says Cannot read property 'addEventListener' of null
const togleList = 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');
togleList.addEventListener('click', () => { if (listDiv.style.display == 'none') { togleList.textContent = 'hide list'; listDiv.style.display = 'block'; }else { togleList.textContent = 'show list'; listDiv.style.display = 'none'; }
});
descriptionButton.addEventListener('click', () => { descriptionP.innerHTML = descriptionInput.value + ':'; });
addItemButton.addEventListener('click', () => { let ul = document.getElementsByTagName('ul')[0]; let li = document.createElement('li'); li.textContent = addItemInput.value; ul.appendChild(li); } );
3 Answers
Steven Parker
229,732 PointsNow that I can see the whole thing, it's an identifier mismatch. The script refers to "addItemButton" (capital "B"), but in the HTML the button class is "addItembutton" (lower-case "b").
There's a similar issue with "removeItemButton" and "removeItembutton".
Steven Parker
229,732 PointsThe script seems OK, perhaps the problem is in the HTML part of the code (not shown here).
You can share your entire project at once if you make a snapshot of your workspace and post the link to it here.
baloch baloch
63 Pointsbaloch baloch
63 Pointsthankx man