-4

I know there are already threads on this but I couldn't find an answer to fit my needs. I'm trying to use external JS to change external CSS. I have a very simple CSS body style:

html, body{
background-color: black;
color: white;
}

To change this with JavaScript I tried:

var bg = document.getElementById("body");
body.style.backgroundColor = white;

But that didn't seem to work. I don't have too much experience with CSS combined with JavaScript so I think the fix is something simple but I can't seem to find an answer. Any help would be appreciated.

2
  • 3
    body.style.backgroundColor = 'white';
    – karaxuna
    Commented Jun 14, 2013 at 12:56
  • 1
    you are using white, and javascript takes it as variable, it should be a string. eg. 'white'.
    – Bhavesh G
    Commented Jun 14, 2013 at 12:58

6 Answers 6

6

You have lots of problems

  1. var bg = document.getElementById("body"); will get <something id="body"> not <body> (but your CSS is matching <body> so presumably that is what you want to change)
  2. body.style.backgroundColor is using a variable called body not bg (which is what you used before.
  3. = white assigns the value of the variable white, not the string "white".

Corrected code:

var bg = document.body;
bg.style.backgroundColor = "white";
2
  • +1 the only other answer to notice the body ID issue, beat me by 11 seconds :)
    – MrCode
    Commented Jun 14, 2013 at 13:00
  • Looks like I'm more rusty than I thought. I tried this code, with adding in bg.style.color = "black"; and the font color changes but not the background. Commented Jun 14, 2013 at 14:01
1

You need to make white a string like "white"

Javascript is reading white as a variable.

1

You're using the wrong variable!

var bg = document.getElementById("body");
bg.style.backgroundColor = "white";
2
  • I guess that the OP wants to change the background color of the body (and not of the element with an ID of body)... Commented Jun 14, 2013 at 12:58
  • 1
    Yes I agree, a lot went wrong here. I would agree with @Quentin's answer.
    – ಠ_ಠ
    Commented Jun 14, 2013 at 12:59
1

replace body.style.backgroundColor = white; with

body.style.backgroundColor = 'white';

Here color name must be a string

To change the body back ground color use

 document.body.style.background = 'white';
0

Has your <body> really got an id="body"?

You can select the body with document.body and use quotes for the colour value:

 document.body.style.backgroundColor = 'white';

Or a hex colour

document.body.style.backgroundColor = '#ffffff';
0

white should be enclosed within single or double quotes

no need to use var bg = document.getElementById("body"); as i dont feel you are using variable bg anywhere

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.