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 Select a Page Element By Its ID

[Error] SyntaxError: Can't create duplicate variable that shadows a global property: 'myHeading' is this video outdate

[Error] SyntaxError: Can't create duplicate variable that shadows a global property: 'myHeading' (anonymous function)

Is the full error code I get when I open up my console. I am using Safari and my code is exactly like the video.

I did changed the constants so they weren't the same as the IDs and it worked. Does this video need to be updated to reflect that or is this unique to safari?

app file /*const myHeading = document.getElementById('myHeading'); const mybutton = document.getElementById('myButton'); const myTextInput = document.getElementById('myTextInput');

myButton.addEventListener('click', () => { myHeading.style.color = myTextInput.value; });*/

index /*<!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> <input type="text" id="myTextInput"> <button id="myButton">Change headline color</button> <script src="app.js"></script> </body> </html> */

1 Answer

Steven Parker
Steven Parker
220,378 Points

That might be unique to Safari.

I just tried it in Chrome and got no error, even when using "strict" mode.

The program functionality is exactly the same with the const lines removed, which proves that the implicit globals do exist. In fact, the program only works because they exist to begin with, because there's a typo where "mybutton" is created using a lower-case name but the event handler is attached to "myButton" (with capital "B").

But there is apparently no restriction in re-using the names in Chrome.