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) Getting a Handle on the DOM A Simple Example

Lee Gentile
Lee Gentile
1,707 Points

Issues inputing JS into the console / .getElementById

Whenever I try to change the color of the heading element in the console I keep getting this error message:

Uncaught TypeError: Cannot read property 'style' of null at <anonymous>:1:37

I literally typed exactly what he did:

document.getElementById('myHeading').style.color = 'red'

I'm using Chrome 56.0.2924.87

any thoughts?

2 Answers

andren
andren
28,558 Points

The command itself is correct. The error message suggests to me that it cannot find any element with an id of myHeading in your webpage.

Can you post the HTML code from your index.html file? The error is likely somewhere within that file.

Lee Gentile
Lee Gentile
1,707 Points

For some reason it won't let me attach a screen shot, but I'm just using his pre made HTML template in Workspace with all the info already put in:

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

I didn't do anything to it except attach the app.js file.

Thanks for the help!

Abhijit Das
Abhijit Das
5,022 Points

it works just fine for me...I'm using chrome 58.0.3029.110 though. codepen link is https://codepen.io/anon/pen/zwMRwQ