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 OverflowIf 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>
Changing background color onclick - JavaScript - SitePoint Forums | Web Development & Design Community
addEventListener and changing background color
html - Changing background-color with onclick-function - Stack Overflow
Q: How to change background color of table cells onClick?
Videos
Hello!
I am having some trouble changing the background color of my table cells onClick and I can't solve it with google (im trying to grab a bunch of cells using getElementsbyClass and change their background color onClick).
Anyone know how to do this or point me in the right direction?
Thanks in advance,
Roger.