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

General Discussion

Background Won't Change

So iv'e started Javascript Foundations and already need help. This code is supposed to change the div background color:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Variables</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Variables</h2>

    <div id="myDiv">
      This is a div with the id of "myDiv"
    </div>
    <script src="myscript.js"></script>
  </body>
</html>

/* JavaScript Foundations: Variables */

var color = red;

document.getElementById('myDiv').style.background = "black";

but nothing is happening?

P.S. both files are in the same folder

4 Answers

function changeBackground(color) { document.body.style.background = color; }

<BODY onload="changeBackground('red');">

since he hasn't used quotes around "red" the function you put there won't work. the color variable is not assigned with the correct syntax. he needs to put the word red in quotes.

I'm assuming the variable assignment for 'color' is included in the myscript.js file? If not, you need to wrap the code at the end of your block there, in script tags. Also, the word 'red' needs to be in quotes. Any word in javascript not put in quotes is usually treated as a keyword or a variable. It looks like, in this case, that you were trying to put red as a color value for the variable color. Color strings needs to always be put in quotes to work.

Wow, i can't believe i missed that....thanks that was it, i will try and keep an eye!

anytime... would you mind voting up my response if it is the best answer?