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 (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,520 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