Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM Making Changes to the DOM Getting and Setting Text with textContent and innerHTML

Amy Tomey
Amy Tomey
11,032 Points

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
STAFF
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
12,919 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 + ':';
})