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

arik
arik
5,791 Points

Uncaught SyntaxError : Unexpected Token <

I used bracket text editor to write my code, and when I checked it using chrome dev tool it showed the message Uncaught SyntaxError : Unexpected Token <, which lead to the first line <! DOCTYPE html> What's the meaning of that? Is there anyone could give a hand to clarify? And This is the code I've written:

<!DOCTYPE html> <html> <head> <title>JavaScript and the DOM</title>

</head> <body> <h1 id="myHeading">JavaScript and the DOM</h1> <p>Making a web page interactive</p> <script type = "text/javascript"> const myHeading = document.getElementById( " myHeading" ); myHeading.addEventListener( "click", () => { myHeading.style.color = "red"; }); </script> </body> </html>

Thank you very much in advance.

3 Answers

0yzh 󠀠
0yzh 󠀠
17,276 Points

Hey Arik,

Looks like you have an extra space in your query for 'myHeading' -- document.getElementById( " myHeading" ).

// remove extra space
const myHeading = document.getElementById( "myHeading" ); 

Hope this helps

arik
arik
5,791 Points

Hey Hui Bui, Thank you very much for your help:) I am really a novice at programming, and never used jsfiddle before. I'll learn how to use that. Thank you for introducing me to jsfiddle:)

0yzh 󠀠
0yzh 󠀠
17,276 Points

No problem! glad I could help.

arik
arik
5,791 Points

I've removed the white space, but the console still send the same error message. What I don't understand is, why I can still launch it using bracket's live preview, but when I tried to use console it sent the error message...What could be wrong?

0yzh 󠀠
0yzh 󠀠
17,276 Points

Hey Arik,

Here's a working jsfiddle example of the code you provided and extra space removed. You can do a code compare to the working example and see what else may be causing the error.