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 Getting and Setting Text with textContent and innerHTML

app.js:6 Uncaught TypeError: Cannot set property 'textContent' of null at HTMLButtonElement.<anonymous> (app.js:6)

I cannot find the error in the code that is causing the problem. It MUST be a tiny little thing...

<!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>    
    <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>
    <script src="app.js"></script>
  </body>
</html>

And the JavaScript:

const input = document.querySelector ('input');
const p = document.querySelector ('description');
const button = document.querySelector ('button');

button.addEventListener ('click', ()=> {
    p.textContent = input.value + ':';
})

Thanks in advance!

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Amy Tomey! You are correct. It is a tiny little thing. When picking by a class name in a querySelector() it must be preceded by a . or full stop/period.

You have this:

const p = document.querySelector ('description');

But you should have:

const p = document.querySelector('p.description');  // Note the p. before the description because you're picking the paragraph with a class of "description"

Hope this helps! :sparkles:

Blake Larson
Blake Larson
13,014 Points

You have all three of your elements you are grabbing with the same description class name.

<!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>    
    <p class="description">Things that are purple:</p>
    <input type="text" class="input">
    <button class="button">Change list description</button>
    <ul>
      <li>grapes</li>
      <li>amethyst</li>
      <li>lavender</li>
      <li>plums</li>
    </ul>
    <script src="app.js"></script>
  </body>
</html>

And just a period before the class name in the selector.

const input = document.querySelector ('.input');
const p = document.querySelector ('.description');
const button = document.querySelector ('.button');

button.addEventListener ('click', ()=> {
    p.textContent = input.value + ':';
})