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 trialMarcus Thomaeus
12,437 PointsFinally, in th ebutton click handler, apply a background color of "red" to the warning div.
Don't know how
var warning = document.getElementById("warning");
let button = document.getElementById('makeItRed');
button.addEventListener("click", function(){});
<!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
231,268 PointsThe 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";
Liudmyla Capezzuto
11,645 PointsThank you!
Tim Abbott
14,206 PointsSteven, 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
231,268 PointsWhile 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.
Tim Abbott
14,206 PointsThank you!
Liudmyla Capezzuto
11,645 PointsLiudmyla Capezzuto
11,645 PointsHi, 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
231,268 PointsSteven Parker
231,268 PointsThe code to change the color needs to go inside your new function (between the braces):
And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.
Mauricio Hernandez
7,208 PointsMauricio Hernandez
7,208 PointsThank you and you and your family have a blessed day.