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
🌐
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
Discussions

Using JavaScript to change an element's background color via CSS
Hey everyone! I’m trying to figure out how to change the background color of an HTML element using JavaScript. I know we can do this with CSS, but I need to make it happen dynamically with JS. Does anyone know the best way to go about this? Here’s what I’ve tried so far: function ... More on community.latenode.com
🌐 community.latenode.com
0
0
May 5, 2025
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
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
Change background color by DOM manipulation
Trying to change the background color of the paragraph element when someone enters a color into the textbox and hits “change color” button. .On page load, a random number of elements will be generated. Random numbers are 1-5 elements. Any advice on how to get this going? More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
September 3, 2021
🌐
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 - Each function has one thing in common: they all target the body. Through the body element (which represents the webpage), we accessed the style.backgroundColor property. The property returns or sets the background color of an element.
🌐
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
Using JavaScript to change the background color involves adding an event listener to a button, which triggers a function to set the background color property of an element, dynamically altering the background when the button is clicked.
Published   August 5, 2025
🌐
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 ...
🌐
Latenode
community.latenode.com › other questions › javascript
Using JavaScript to change an element's background color via CSS - JavaScript - Latenode Official Community
May 5, 2025 - Here’s what I’ve tried so far: function changeColor(elementId, newColor) { let targetElement = document.getElementById(elementId); targetElement.style.backgroundColor = newColor; } // Usage changeColor('myDiv', 'blue'); This kind of works, ...
🌐
Educative
educative.io › answers › how-to-change-background-color-in-javascript
How to change background color in JavaScript
There are two simple ways to change the background color of a web page using JavaScript:
Find elsewhere
🌐
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> ...
🌐
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;
}

🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Change background color by DOM manipulation - JavaScript - The freeCodeCamp Forum
September 3, 2021 - Trying to change the background color of the paragraph element when someone enters a color into the textbox and hits “change color” button. .On page load, a random number of elements will be generated. Random numbers a…
🌐
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 - What happens here is every time we click the button the value of i gets incremented by 1. But hold on! Why not just use a loop inside the “changeBackground” function instead? Why do we need to use 2 functions? Well, here’s the thing. We need the background color to change each time we press the button.
🌐
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
🌐
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; } ...
🌐
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
🌐
GitConnected
levelup.gitconnected.com › change-background-color-using-javascript-e60d172e9124
Change Background Color Using JavaScript | by Raja Tamil | Level Up Coding
January 16, 2023 - Change Background Color Using JavaScript There are a few ways you can change the background color of an HTML Element dynamically in JavaScript. Using style.backgroundColor property Using …
🌐
SoftAuthor
softauthor.com › home › javascript › change background color using javascript
Change Background Color Using JavaScript
December 29, 2025 - There are a few ways you can change the background color of an HTML Element dynamically in JavaScript. ... You can use the style.backgroundColor property directly in JavaScript to change a background colour of an HTML element.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › design-background-color-changer-using-html-css-and-javascript
Design Background color changer using HTML CSS and JavaScript - GeeksforGeeks
Background color changer is a project which enables to change the background color of web pages with ease. There are color boxes on a web page when the user clicks on any one of them, then the resultant color will appear in the background of ...
Published   August 5, 2025