JavaScript JavaScript and the DOM Getting a Handle on the DOM Select a Page Element By Its ID

Cody Tapp
Cody Tapp
9,622 Points

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,787 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.

Cody Tapp
Cody Tapp
9,622 Points

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,787 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.