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

The code doesn't work in Safari 10.0.3

Hi! I got an error with the code in Safari: Code:

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

myHeading.addEventListener('click', () => {
    console.log('test')
    myHeading.style.color = 'red';
});

Error:

SyntaxError: Can't create duplicate variable that shadows a global property: 'myHeading'

And event handling doesn't work. In Chrome I've the same code working perfictly fine. Should we use ES6 recomendations if it's doesn't work well even in the latest version of Safari?

Thank you!

4 Answers

Hey gennady,

It's a bug in Safari. Change const to var and it should work.

Ruben Dario Avila Home
Ruben Dario Avila Home
8,806 Points

It's a Bug in Webkit Safari Core, because the name of the const are the same of the ID. Rename Const and it works.

Jeremy Hamilton
Jeremy Hamilton
7,229 Points

You can also just rename the constant and it'll work.

Interesting, I just ran into the same problem using Chrome (beta v. 59.0.3071.36) and IE/Edge. Thanks to everyone here for posting. I renamed variable(constant) to "blurb" from "myHeading" and it worked perfectly.

const blurb = document.getElementById('myHeading');

blurb.addEventListener('click', () => {
    blurb.style.color = 'red';
});