Modify the JavaScript property document.body.style.background.
For example:
function changeBackground(color) {
document.body.style.background = color;
}
window.addEventListener("load",function() { changeBackground('red') });
Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.
Answer from user7094 on Stack OverflowModify the JavaScript property document.body.style.background.
For example:
function changeBackground(color) {
document.body.style.background = color;
}
window.addEventListener("load",function() { changeBackground('red') });
Note: this does depend a bit on how your page is put together, for example if you're using a DIV container with a different background colour you will need to modify the background colour of that instead of the document body.
You don't need AJAX for this, just some plain JavaScript setting the background-color property of the body element, like this:
document.body.style.backgroundColor = "#AA0000";
If you want to do it as if it was initiated by the server, you would have to poll the server and then change the color accordingly.
html - How to change in javascript background color from css - Stack Overflow
Changing the style attribute background-color? - JavaScript - SitePoint Forums | Web Development & Design Community
How to change background color using JavaScript
How to change background color with JS
Videos
You are attempting to set the background colour of the <style> tag. This is not how it works. Instead of setting the style of the <style> tag, you should set the style of the body itself. This W3Schools Article gives an explanation if you need one.
<head>
<style>
body {
background-color: pink;
}
</style>
</head>
<body id="1">
<script>
document.getElementById("1").style.backgroundColor = "brown";
</script>
</body>
It's also worth noting you don't need to assign the element to a variable unless you are going to use it later.
Use document.body.style.backgroundColor = "brown";, you can't put ids' or classes on non-elements. Try this;
var myVar = document.body.style.backgroundColor = "brown";
body {
background-color: pink;
}
This selects the body element, you could also put id = "1" on the body element.
I keep getting an error telling me that newColor is undefined, which I thought thats what I did in the Javascript. I'm just lost. Any help would be appreciated.
HTML
<!DOCTYPE html>
<html> <head> <title>Background Color Change</title> <link rel="stylesheet" href="backgroundColor.css"> </head> <body id="body"> <button type="button" onclick="newColor()" id="colorChange"><b>Click Me</b></button> <script type="text/javascript" href="backgroundColor.js"></script> </body> </html>
Javascript
function newColor(){
var changeColor= document.getElementById("body");
changeColor.style.backgroundColor('green');
}
CSS
#colorChange:hover{
background-color: red;
}
#colorChange{
background-color: green;
}
#body{
background-color:blue;
text-align:center;
}