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

Event listener is not working.

My function appears to not be recognized although I typed exactly how Guil typed the function. The button appears, but the listener is not working.

Here is the index.html file

<!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> <button id="myButton">Make heading red</button> <script src="app.js"></script> </body> </html>

Here is the app.js file

app.js

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

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

Dimitar Dimitrov
Dimitar Dimitrov
11,800 Points

The code you wrote is perfectly fine i tested it and it turn the heading red when you click the button like in the video, maybe you can check the browser console for an error which can point you in some direction, and make sure you are looking at the right webpage for your current Workplace.

dimitar dimitrov When I go to Chrome's console, it looks like it gives me a Failed to load resource: the server responded with a status of 404 (Not Found) app.js:1 error

1 Answer

Dimitar Dimitrov
Dimitar Dimitrov
11,800 Points

Well in that case the only thing that comes in mind is that the path to link the script with the page is somehow incorrect, maybe you have moved the app.js file check your file path again. In the Chrome console you can click on the error in the right hand side of the message that will lead you to the line in your code that has the error.