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

I get this error in the console:

After typing in everything correctly I get this message.

[Error] SyntaxError: Can't create duplicate variable that shadows a global property: 'myHeading'
Ben Schroeder
Ben Schroeder
22,818 Points

Can you please copy and paste your script?

4 Answers

Nathan Gallagher
Nathan Gallagher
21,518 Points

This is apparently a problem Safari has with declaring a let/const variable that shares the same name as a selected id attribute. I had some variables declared with const as you did and got the same error. Changed to let and still got it. Changed to var and it worked.

Thanks Nathan :)

Thanks Nathan. I was experiencing the same issue on Safari. Anyone know why its not working even with let? strange error.

const myHeading = document.getElementById('myHeading');
const myButton = document.getElementById('myButton');
const myTextInput = document.getElementById('myTextInput');

myButton.addEventListener('click', () => {
 myHeading.style.color = 'red';

});

Same here. But it only appears in safari not in chrome

I have this problem in Safari. With Chrome it is working.

Jeremy Hamilton
Jeremy Hamilton
7,229 Points

I has to do with how the window element has attributes for each HTML ID. It's known, but faster just to change to var or change the name of the constant.