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.
Need help with changing the background color using javascript
How to change background color with JS
How to change background color using JavaScript
html - javascript change background color on click - Stack Overflow
Videos
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;
}
If you want change background color on button click, you should use JavaScript function and change a style in the HTML page.
function chBackcolor(color) {
document.body.style.background = color;
}
It is a function in JavaScript for change color, and you will be call this function in your event, for example :
<input type="button" onclick="chBackcolor('red');">
I recommend to use jQuery for this.
If you want it only for some seconds, you can use setTimeout function:
window.setTimeout("chBackColor()",10000);
You can set the background color of an object using CSS.
You can also use JavaScript to attach click handlers to objects and they can change the style of an object using element.style.property = 'value';. In the example below I've attached it in the HTML to a button but the handler could equally have been added to the body element or defined entirely in JavaScript.
body {
background-color: blue;
}
<button onclick="document.body.style.backgroundColor = 'green';">Green</button>