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 trial

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Appending Nodes

I keep getting an error and I dont see the problem

The error I keep receiving is " Uncaught TypeError: Cannot read property 'addEventListener' of null at app.js:24 " from Chrome. Line 24 is this one "addItemButton.addEventListener("click", () => {" It is selecting the ul because when I changed the tag name selector to a query selector and made a class I was still getting the same error

const toggleList = document.querySelector("#toggleList");
const listDiv = document.querySelector(".list")
const descriptionInput = document.querySelector("input");
const descriptionP = document.querySelector("p.description");
const descriptionButton = document.querySelector("button.description");
const addItemInput = document.querySelector("input.addItemInput");
const addItemButton = document.querySelector("input.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.getElementsByTagName("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>
Adam Duffield
Adam Duffield
30,494 Points

Hi Paul, I've not been on this in years but thought I'd have a cheeky nosy.

Looks like your looking for input.addItemButton when infact the html element with the class"addItemButton" is of type <button> not of type <input> hence you calling ".addEventListener" on null. If you console.log(addItemButton) you will find that it is null. Whereas if you change this...

const addItemButton = document.querySelector("input.addItemButton");

to this...

const addItemButton = document.querySelector("button.addItemButton");

Your code should work.

Also, it's worth noting in my 6+ years of coding (I also learnt to code on Treehouse by the way!) I have never seen html type or class attributes added like that. With spaces inbetween = and quotation marks that is. I wasn't even sure if it was valid at first! People will pick you up on that and ask you to have no spaces at eitherside of the equals in class = "addItemButton".

Best of luck! Adam

Adam Duffield Thank you very much! I had no clue about the html part I just thought it would be easier to read. I will try to work on that as I was to use best practices. What do you do? Was it hard to go from teamtreehouse to a jr position?

1 Answer

Steven Parker
Steven Parker
231,269 Points

There is no input element with the class "addItemButton" to select. You probably meant to write "button" instead of "input":

const addItemButton = document.querySelector("button.addItemButton");

Thank you Steven Parker for helping me again!