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) 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
210,472 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
210,472 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.

Mauricio Hernandez
Mauricio Hernandez
5,377 Points

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
210,472 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.