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);
Answer from Saeed Rahmani on Stack Overflow
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-change-background-color-on-click
Change background color on click using JavaScript | bobbyhadz
And here is the related JavaScript code. index.js · Copied!const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const box = document.getElementById('box'); box.style.backgroundColor = 'coral'; // 👇️ optionally change text color // box.style.color = 'white'; }); The code for this article is available on GitHub ·
Discussions

Changing background color onclick - JavaScript - SitePoint Forums | Web Development & Design Community
After the image is clicked, I would like to change the background to a different color. How would I implement this? Clicking on the jacket would change the background of black to a different color. I found this type o… More on sitepoint.com
🌐 sitepoint.com
0
October 28, 2018
addEventListener and changing background color
Hello, I’m having some difficulty understanding events and calling functions inside addEventListener I’m attempting to change the background color of the body of the page each time I click on the element. The background changes once and then remains red, never changing back to blue. More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
0
May 25, 2018
html - Changing background-color with onclick-function - Stack Overflow
I want to change the background color of a class by clicking on a class. The code that I tried doesn't work. Thank you in advance HTML: More on stackoverflow.com
🌐 stackoverflow.com
February 24, 2017
Q: How to change background color of table cells onClick?
Are you using jQuery or another JavaScript library at all, or just trying to do it with vanilla JavaScript? var cells = document.getElementsByClass('blahblah'); for (var i = 0; i < cells.length; i++) { cells.attachEventListener("click", setBackgroundColor); } function setBackgroundColor() { this.style.backgroundColor = 'green'; } That should do the trick, but it's urgly. If you had jQuery already, I'd recommend doing it as a $('table').on('click', 'td.className', setBackgroundColor); More on reddit.com
🌐 r/javascript
11
0
September 18, 2012
🌐
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 › 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   April 30, 2025
🌐
SitePoint
sitepoint.com › javascript
Changing background color onclick - JavaScript - SitePoint Forums | Web Development & Design Community
October 28, 2018 - After the image is clicked, I would like to change the background to a different color. How would I implement this? Clicking on the jacket would change the background of black to a different color. I found this type of code that might work in here. function changebackColor(){ document.body.style.backgroundColor = "black"; document.querySelector("class").style.backgroundColor = "red"; } (function iife() { "use strict"; function hide(el) { el.classList.add("hide"); } fu...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-change-the-background-color-after-clicking-the-button-in-javascript
How to Change the Background Color after Clicking the Button in JavaScript?
November 22, 2024 - To change the background color after clicking the button in JavaScript, we have used backgroundColor property of DOM style object.
🌐
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 - document.body.style.backgroundColor = 'white'; in the defaultBgColor() function sets the background color of body to white. Next, you'll assign each function to their respective button using the onclick attribute in your HTML file.
Find elsewhere
🌐
YouTube
youtube.com › watch
Change Background Color onclick using JavaScript-2024 - YouTube
Change Background Color onclick using JavaScript-2024.In this tutorial we will see how to Change Background Color onclick using JavaScript. The getComputedSt...
Published   March 30, 2024
🌐
Coding Artist
codingartistweb.com › home › javascript › change background color on click
Change Background Color On Click | Coding Artist
May 19, 2023 - It shows how to listen for specific events, such as a button click or page load, and associate them with corresponding functions. Dynamically changing styles: The JavaScript code utilizes the document.body.style property to dynamically change ...
🌐
freeCodeCamp
forum.freecodecamp.org › t › addeventlistener-and-changing-background-color › 193023
addEventListener and changing background color - The freeCodeCamp Forum
May 25, 2018 - Hello, I’m having some difficulty understanding events and calling functions inside addEventListener I’m attempting to change the background color of the body of the page each time I click on the <button> element. The …
🌐
Linux Hint
linuxhint.com › change-background-color-after-clicking-button-javascript
How to Change the Background Color After Clicking ...
August 12, 2022 - Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
YouTube
youtube.com › howtocodeschool
Change Background Color onclick using JavaScript - YouTube
Source Code on our Website: HowToCodeSchool.comIn this tutorial we will see how to Change Background Color onclick using JavaScript. The getComputedStyle() m...
Published   January 30, 2021
🌐
YouTube
youtube.com › online tutorials
Change Background Color On Click - HTML CSS and Javascript - YouTube
Enroll My Course : Next Level CSS Animation and Hover Effects https://www.udemy.com/course/css-hover-animation-effects-from-beginners-to-expert/?referralCode
Published   July 21, 2018
Views   80K
🌐
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
🌐
StackHowTo
stackhowto.com › home › web development › javascript › how to change the background color with javascript
How to change the background color with JavaScript - StackHowTo
October 12, 2021 - You can simply use the background property to change the background color after clicking the button. This property is used to set the background color of an element. <!DOCTYPE HTML> <html> <head> <script> function changeColor(color) { ...
🌐
Codeflare Limited
codeflarelimited.com › home › cascading style sheet › javascript change background color on click
JavaScript Change Background Color on Click
June 24, 2022 - In this tutorial, we are going to change the background color of a webpage to the one supplied by the user in the input field.
🌐
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; } // Function to change heading background color function changeHeadingBg(color){ document.getElementById("heading").style.background = color; } </script> </head> <body> <h1 id="heading">This is a heading</h1> <p>This is a paragraph of text.</p> <hr> <div> <label>Change Webpage Background To:</label> <button type="button" onclick="changeBodyBg('yello
🌐
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