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 trialKurt Coleman
2,967 Pointsdocument.querySelector("h1").textContent = "🔥BOOM!🔥";
This statement generates an error. TypeError: Cannot set property 'textContent' of null
4 Answers
Cameron Childres
11,820 PointsDo 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>
Sorin Chircu, SC OSF GLOBAL SERVICES SRL Calea Plevnei nr.159, Sector 6, 060014, Bucuresti. VAT: RO12900448
14,547 PointsFrom 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.
Muhammad Khan
Courses Plus Student 8,772 PointsI 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
2,967 PointsYes. 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
11,820 PointsWould 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.
Kurt Coleman
2,967 PointsKurt Coleman
2,967 PointsI'm lost. Don't worry about it.