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 Basics Hello, JavaScript! JavaScript Practice Challenge

Kurt Coleman
Kurt Coleman
2,967 Points

document.querySelector("h1").textContent = "🔥BOOM!🔥";

This statement generates an error. TypeError: Cannot set property 'textContent' of null

Kurt Coleman
Kurt Coleman
2,967 Points

I'm lost. Don't worry about it.

4 Answers

Cameron Childres
Cameron Childres
11,820 Points

Do you have an <h1> element in your HTML? If it's not there then document.querySelector("h1") won't find anything to put your text in to.

Example that works:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <h1></h1>
    <script src="js/script.js"></script>
  </body>
</html>
document.querySelector("h1").textContent = "🔥BOOM!🔥";

I receive your same error when I remove the <h1> element:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
  </head>
  <body>
    <script src="js/script.js"></script>
  </body>
</html>

From what I've seem a problem can also appear if you add the <script> to the head, as it'll look for the <h1> element there, maybe that's the problem.

I received the same error at line No. 10, but when I added the <h1></h1> tag in my HTML file, the error gone and the the program is running good and it gives the desired output. Thanks.

Kurt Coleman
Kurt Coleman
2,967 Points

Yes. There was an <h1> element in the HTML. This line of code was supplied. I didn't write it. It generated an error. I looked for the H1 element first.

Cameron Childres
Cameron Childres
11,820 Points

Would you mind sharing your code so I can have a closer look? You can copy and paste it here (using the markdown cheatsheet linked below the comment field to make it look pretty and easily readable) or take a snapshot from your workspace (camera icon in the top right) and post the URL it generates.