+ 5

How to change the background color of the page using JavaScript?

I was just thinking of a project of background changer!! In it I tried to design a code in which the background is directly changed by clicking a button!! But for that I will have to link it to JavaScript, but for the background color, it's the CSS part!! How to I directly change the background color of the page using JavaScript.

17th Mar 2017, 3:40 PM
Shreyas Sable
Shreyas Sable - avatar
2 odpowiedzi
+ 5
Try this: <button class="whatever​" onclick="document.body.style.backgroundColor = 'red'">click to change BG color</button>
17th Mar 2017, 4:16 PM
CHMD
CHMD - avatar
+ 3
var button = document.getElementById("whatever"); button.onclick = function(){ document.body.style.backgroundColor = "whatever-you-choose"; };
19th Feb 2018, 10:49 AM
Vikash Pal
Vikash Pal - avatar