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) Responding to User Interaction Adding an Event Listener

Finally, in th ebutton click handler, apply a background color of "red" to the warning div.

Don't know how

app.js
var warning = document.getElementById("warning");

let button = document.getElementById('makeItRed'); 
button.addEventListener("click", function(){});
index.html
<!DOCTYPE html>
<html>
    <head>
        <title>Adding an Event Listener</title>
    </head>
    <link rel="stylesheet" href="style.css" />
    <body>
        <div id="warning">
            Warning: My background should be red!
        </div>
        <button id="makeItRed">Make It Red!</button>
        <script src="app.js"></script>
    </body>
</html>

4 Answers

Steven Parker
Steven Parker
231,236 Points

The code has already selected the element and saved it in the variable "warning". So now, inside your new function you just need to assign the color to the appropriate element attribute:

    warning.style.backgroundColor = "red";

Hi, Steven! It doesn't work( It says: "The background color for warning is already 'red'!" var warning = document.getElementById("warning");

let button = document.getElementById('makeItRed');

button.addEventListener('click', function(){}); warning.style.backgroundColor = "red"; Could you help me please! Thank you!

Steven Parker
Steven Parker
231,236 Points

The code to change the color needs to go inside your new function (between the braces):

button.addEventListener("click", function() {
    warning.style.backgroundColor = "red";
});

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Thank you and you and your family have a blessed day.

Thank you!

Tim Abbott
Tim Abbott
14,206 Points

Steven, also thanks for this. I've had a similar problem and obviously haven't quite understood something. My code was basically the same as yours, except that I used "background-color" (standard CSS property) and not "backgroundColor". I reviewed my notes from the previous Styling Elements video where Guil also uses backgroundColor (I think for the first time), although the change wasn't highlighted and I didn't pick up on the slight variation in syntax. Do you know the reasons behind this? Is it normal to change the syntax like this? Thanks in advance. Tim

Steven Parker
Steven Parker
231,236 Points

While fine in CSS, hyphens are not allowed in identifiers in JavaScript. So the "kebab" notation used in CSS is translated into "camel case" for JavaScript.