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 Overflow
🌐
Tabnine
tabnine.com › home › how to change the background color with javascript
How to Change the Background Color with JavaScript - Tabnine
July 25, 2024 - The code uses this property to assign the value “yellow” to the element, changing its background color to yellow. Note: Using the style property will add inline styling to the element. Inline CSS is the most specific level of CSS that can be applied to an object, applying to an individual element itself. It thus overrides most other CSS declarations for an element. This approach centers on using element properties in JavaScript ...
Discussions

Need help with changing the background color using javascript
change your href to src in script tag. also, in javascript. it should be changeColor.style.backgroundColor = 'green'; More on reddit.com
🌐 r/learnprogramming
3
2
February 1, 2020
How to change background color with JS
Aryaman Dhingra is having issues with: I have some test code here, in which I am trying to change the background color of a page with the click of a button. The code seems correct to ... More on teamtreehouse.com
🌐 teamtreehouse.com
1
July 10, 2017
How to change background color using JavaScript
Piotr Manczak is having issues with: It doesn't work. It doesn't say why. Any ideas what I'm doing wrong? More on teamtreehouse.com
🌐 teamtreehouse.com
2
February 1, 2019
html - javascript change background color on click - Stack Overflow
If you want it only for some seconds, you can use setTimeout function: ... Sign up to request clarification or add additional context in comments. ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › jsref › prop_style_backgroundcolor.asp
HTML DOM Style backgroundColor Property
The backgroundColor property sets or returns the background color of an element. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
W3Schools
w3schools.com › cssref › tryit.php
Change background-color with JavaScript
❯Run Code Ctrl+Alt+R Save Code Ctrl+Alt+A Change Orientation Ctrl+Alt+O Change Theme Ctrl+Alt+D Go to Spaces Ctrl+Alt+P
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-change-the-background-color-after-clicking-the-button-in-javascript
How to change the Background Color after Clicking the Button in JavaScript? - GeeksforGeeks
The background color of the entire <body> is changed to light gray (#ccc) using jQuery's .css() method. The empty <h2> with ID "GFG" is updated to show the text “Background Color Changed!” using jQuery’s .text() method.
Published   August 5, 2025
🌐
freeCodeCamp
freecodecamp.org › news › how-to-change-background-color-with-javascript
How to Change Background Color with JavaScript – BG Color in JS and HTML
June 28, 2024 - To change the background color of an element with JavaScript, you can use the element's style property: ... function setBgGreen() { document.body.style.backgroundColor = 'green'; } ​ function setBgRed() { document.body.style.backgroundColor ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript change background color
How to Change the Background Color in JavaScript | Delft Stack
March 11, 2025 - In this code, we define a function getRandomColor() that generates a random HEX color code. When the button with the ID randomColorButton is clicked, the onclick event calls this function, and the background color is updated accordingly.
Find elsewhere
🌐
Educative
educative.io › answers › how-to-change-background-color-in-javascript
How to change background color in JavaScript
The function is responsible for ... are two simple ways to change the background color of a web page using JavaScript: Using the style.backgroundColor property....
🌐
Reddit
reddit.com › r/learnprogramming › need help with changing the background color using javascript
r/learnprogramming on Reddit: Need help with changing the background color using javascript
February 1, 2020 -

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;
}

🌐
Team Treehouse
teamtreehouse.com › community › how-to-change-background-color-with-js
How to change background color with JS (Example) | Treehouse Community
July 10, 2017 - <body> <div> <input type='submit' id='red' class='buttons' value='Red'> <input type='submit' id='blue' class='buttons' value='Blue'> <input type='submit' id='green' class='buttons' value='Green'> <input type='submit' id='purple' class='buttons' value='Purple'> </div> <script> var blue = document.querySelector('#blue'); var red = document.querySelector('#red'); var green = document.querySelector('#green'); var purple = document.querySelector('#purple'); blue.addEventListener('click', () => { document.body.style.backgroundColor = 'blue'; }); red.addEventListener('click', () => { document.body.st
🌐
Sololearn
sololearn.com › en › Discuss › 2442349 › solvedhow-can-i-change-background-color-of-a-div-element-using-javascript
[Solved]How can I change background color of a div element using javascript? | Sololearn: Learn to code for FREE!
I tested and got similar code to ... Playground: JS: document.addEventListener('DOMContentLoaded', function() { var div1=document.getElementById("div1"); div1.style.backgroundColor="#ffff00"; // yellow }); HTML: <!DOCTYPE html> ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-change-the-background-color-of-a-web-page-using-javascript.php
How to Change the Background Color of a Web Page Using JavaScript
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Change the Background Color with JavaScript</title> <script> // Function to change webpage background color function changeBodyBg(color){ document.body.style.background = color; } ...
🌐
Medium
kumuthudulnath.medium.com › lets-learn-javascript-by-making-a-simple-application-c7fcce6f9f30
How to make a Background Color changing Application Using JavaScript and HTML | by Kumuthu_Dulnath | Medium
February 17, 2021 - If it is equal, we reassign the value of I back to 0. Cause if we exceed the parameters of our array then the colors will stop changing cause our we’re not cycling back to the beginning. Next, we assign a variable named color and assign the value of colors[i]. In line 9 we assign a new background color to our HTML page. Instead of assigning the color variable you can directly assign colors[i] to it and reduce your code length a bit. And finally, were going to return the value of our input parameter (which is i in this case). Then we have the function which we’ll actually be calling, the “Increment” function.
🌐
C# Corner
c-sharpcorner.com › blogs › change-the-background-color-of-a-button-with-javascript1
Change The Background Color of a Button with JavaScript.
Use the button.style.backgroundColor property to set the background color of the button. For example: ... function changeColor() { const button = document.getElementById("myButton"); button.style.backgroundColor = "red"; }
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-dom-exercise-3.php
JavaScript DOM: Set the background color of a paragraph - w3resource
July 18, 2025 - // Function declaration for ... (@w3resource) on CodePen. ... Write a JavaScript function that cycles through an array of background colors for the paragraph each time a button is clicked....
🌐
W3Resource
w3resource.com › javascript-exercises › event › javascript-event-handling-exercise-3.php
JavaScript mouse enter event - Change background color
July 10, 2025 - <!DOCTYPE html> <html> <head> <style> .my-element { width: 200px; height: 200px; background-color: lightgray; } </style> </head> <body> <div id="myDiv" class="my-element"></div> <script> function changeBackgroundColor(elementId, color) { const ...
🌐
YouTube
youtube.com › watch
How to change background color in javascript with one button | JavaScript Tutorial - YouTube
How to change background color in javascript with one button | JavaScript TutorialIn this video we will explore how to change the background color in JavaScr...
Published   June 1, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › design-background-color-changer-using-html-css-and-javascript
Design Background color changer using HTML CSS and JavaScript - GeeksforGeeks
Inside the CSS file, the colorbox ... javascript, we are calling a function bgchange, which contains an array of colors and it is applied to the background when the user clicks on the color....
Published   August 5, 2025