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
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
W3Schools
w3schools.com › cssref › tryit.php
Change background-color with JavaScript
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
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
Changing the background color after clicking a button in JavaScript involves attaching a click event listener to the button. When the button is clicked, it triggers a function that changes the background color of a specific element (like the page or a section of it). This creates a simple and interactive effect that enhances the user ...
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 - You can style elements with JavaScript using the element's style property. In this article, you'll learn how to change background color using JavaScript. Here's what the mini project you'll build looks like: In the image above, each button changes t...
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript change background color
How to Change the Background Color in JavaScript | Delft Stack
March 11, 2025 - The background color of the webpage will change to a random color each time the button is clicked. 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. This method not only showcases JavaScript’s capabilities but also keeps users ...
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 altering the color of the document’s body element from black to crimson using the document.body.style.backgroundColor property. There are two simple ways to change the background color of a web page using JavaScript:
🌐
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 - 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 ...
🌐
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 ... div1.style.backgroundColor="#ffff00"; // yellow }); HTML: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div id="div1">Hello</div> </body> </html> ... It was just an example code. Actually neither the id of Div is div1 nor I need to set its colour as white. In my code, it's the header tag and I am creating a function to change its color Html : line 8 Javascript : line 19 ...
🌐
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
PHP Array Functions PHP String ... PHP MySQLi Functions PHP Filters PHP Error Levels ... You can easily change the background color of a webpage i.e. the <body> element or any other element dynamically by using its style property in JavaScript....
🌐
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 - Hello there, fellow aspiring web developer. In this post were going to create a simple application using JavaScript and in doing so get…
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-dom-exercise-3.php
JavaScript DOM: Set the background color of a paragraph - w3resource
July 18, 2025 - Write a JavaScript function that applies a random background color from a predefined palette to the paragraph on button click. Write a JavaScript function that changes the paragraph’s background color based on the time of day (morning, afternoon, evening).
🌐
W3Resource
w3resource.com › javascript-exercises › event › javascript-event-handling-exercise-3.php
JavaScript mouse enter event - Change background color
July 10, 2025 - Learn how to use JavaScript to change the background color of an element when the mouse enters it. Enhance your web development skills with this interactive JavaScript function.
🌐
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
There are color boxes on a web ... color will appear in the background of the web page. It makes web pages look attractive. ... Prerequisites: Basic knowledge of HTML, CSS, and JavaScript is needed. The project contains HTML, CSS, and JavaScript files. The HTML file adds structure, followed by styling using CSS and JavaScript adds functionality to it. Here is the preview image of the background changer we are going ...
Published   August 5, 2025