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 Working with Strings Display the Value of a String on a Page

Paige Hand
Paige Hand
6,269 Points

my .innerHTML is throwing an error (im on vs code): Cannot set properties of null (setting 'innerHTML')

My code is exactly the same as the tutorial, it might be something in my VScode application rather than a problem with the code itself.

my code: const stringToShout = prompt('what do you want to shout?');

const shout = stringToShout.toUpperCase();

const shoutMessage = <h2>the message to shout is: ${shout}!!!</h2>; (there are back-ticks present they're not showing here though.)

document.querySelector('main').innerHTML = shoutMessage;

the error: Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')


What does your html file look like? Does it have a main element?

2 Answers

Try moving <script src="js/javascript-basics.js"></script> after </main> so that the main element is loaded before the javascript is run.

Paige Hand
Paige Hand
6,269 Points

oh thank god!!! i used:

    <script src="js/javascript-basics.js" defer></script>

which i does the same thing. THANK YOU SO MUCH!!

Paige Hand
Paige Hand
6,269 Points

@jb30 yes it does, my html looks like this:

<!DOCTYPE html>



    <meta charset="utf-8">
    <title>JavaScript Basics</title>
    <link href="css/javascript-basics.css" rel="stylesheet">
    <script src="js/javascript-basics.js"></script>




        <h1>Hello, JavaScript!</h1>