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

Bug in my browser or is my code messed up somehow?

so over this video and the last one ive been having trouble using addEventListener and getting the color of the heading to actually change here is my code: index.html:

<!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> <p>Things that are purple:</p> <ul> <li>grapes</li> <li>amethyst</li> <li>lavender</li> <li>plums</li> </ul> <script src="app.js"></script> </body> </html>

app.js:

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

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

ive looked it over char by char and i cant find where im messing up, any help would be greatly appreciated! thank you in advance

1 Answer

Hi Travis!

I ran this:

<!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>
    <p>Things that are purple:</p>
    <ul>
      <li>grapes</li>
      <li>amethyst</li>
      <li>lavender</li>
      <li>plums</li>
    </ul>

  <script>
    const myHeading = document.getElementById('myHeading');

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

  </body>
</html>

Here (replace all the code in the left pane, run it, and then click the header):

https://www.w3schools.com/js/tryit.asp?filename=tryjs_array

And it worked fine (no errors in the console).

"JavaScript and the DOM" turned red (when clicked).

The only difference is my JS code is in a script tag, not an app.js file.

Notice that since my code is at the bottom of the page, it execute after the DOM loads.

That's important (but your app.js file is in the same location, so it should work - It might just be a path issue!?!

I hope that helps.

Stay safe and happy coding!