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 trialsam poplack
9,467 Pointsnull is not an object
So Im working on the DOM part and on addItemButton.addEventListene I get the error null is not an object. Can someone explain to me the error im getting
addItemButton.addEventListener('click', () => { let ul = document.getElementsByTagName('ul')[0]; let li = document.createElement('li'); li.textContent = addItemInput.value; ul.appendChild(li); addItemInput.value = '';
2 Answers
Neil McPartlin
14,662 PointsHi Sam,
The snippet of code you have pasted here seems to be OK, it's just missing the final line containing... });
I assume you are seeing the error in your browser Javascript console. Does it also reference a filename and line number where the actual problem can be found? It could be elsewhere in your app.js file. Feel free to paste your entire file here but please use the instructions found in the 'Markdown Cheatsheet' at the bottom of this post.
sam poplack
9,467 Pointsconst toggleList = document.getElementById('toggleList'); const listDiv = document.querySelector('.list'); const input = document.querySelector('input.description'); const p = document.querySelector('p.description'); const button = document.querySelector('button.description'); const addItemInput = document.querySelector('input.addItemInput'); const addItemButton = document.querySelector('input.addItemButton'); const removeItemButton = document.querySelector('input.removeItemButton');
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';
}
});
button.addEventListener('click', () => { p.innerHTML = input.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);
}); This is my code. This is the code "addItemButton.addEventListener" that is giving me the error
Neil McPartlin
14,662 PointsNeil McPartlin
14,662 PointsSam, as previously requested, in future posts, please post your code as described in the 'Markdown Cheatsheet'. In this way, it is easier for others to help you.
I have done this for you, see below.
Your browser JavaScript console will have reported the problem affects the addItemButton click event listener and sure enough, the problem is with the addItemButtom declaration at the top of the app.js file. You just need to replace 'input' with 'button' to fix this. The 'Add item' button now functions correctly.
However, note that you now have a similar but different error in your JavaScript console and this time it refers to your removeItemButton feature. I have commented these entries out as I see you have only just started adding this feature. Now you should see no error message in your JavaScript console.