JavaScript JavaScript and the DOM Making Changes to the DOM Appending Nodes

Luis Diaz
Luis Diaz
Front End Web Development Techdegree Student 10,659 Points

Getting UnUncaught TypeError: Cannot read property 'addEventListener' of null

Hey guys although I've checked for any typos in my code I keep getting this is error in the console. I could use a fresh set of eyes to look over it. Thanks!

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');

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 + ':'; });

addItemButton.addEventListener('click', () => { let ul = document.getElementsByTagNameName('ul')[0]; let li = document.createElement(li); li.textContent = addItemInput.value; ul.appendChild('li'); });

<!DOCTYPE html> <html> <head> <title>JavaScript and the DOM</title> <link rel="stylesheet" href="css/style.css"> </head> <body> <h1 id="myHeading">JavaScript and the DOM</h1> <p>Making a web page interactive</p> <button id="toggleList">Hide List</button> <div class="list"> <p class="description">Things that are purple:</p> <input type="text" class="description"> <button class"description">Change list description</button> <ul> <li>grapes</li> <li>amethyst</li> <li>lavender</li> <li>plums</li> </ul> <input type="text" class="addItemInput"> <button class"addItemButton">Add Item</button> </div> <script src="app.js"></script> </body> </html>

1 Answer

You have 2 syntax errors in your html, one is "<button class"description">Change list description.</button>" - you forgot to put the = sign after class. The other one is "<button class"addItemButton">Add Item</button>" - same problem.

You can check your html code in the future at https://validator.w3.org/#validate_by_input

Hope this helps!

Luis Diaz
Luis Diaz
Front End Web Development Techdegree Student 10,659 Points

Oh wow it totally skipped my mind to validate the code. Thank you so much for the help, it was driving me nuts!