Change BG color on click!

Change BG color on click!

molkman
Owner of George Washington's Prototype Mittens

Marine Warfare Corporal
Find the Hole Participation Medal
Find the Hole II Participation Medal
Tasty Br�twurst Medal
2005 May 2 • 2066
404 ₧
Hey!
I want to make a webpage in which the background color (or maybs even the image) is changed whenever you click. I mean, not when you click a button or a certain area, but whenever you click anywhere, just whenever you do a click in general.

Is that possible? I looked for that on the interwebs, but I couldn't find the right code to adress like "clicking in general".


LUV.
LET LOVE REIGN
 
 
 
2015 Sep 28 at 08:41 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Should be able to get it with document.onclick as long as nobody stops propagation before the click gets there.

Try it:
https://jsfiddle.net/hq0c2604/1/

Codes:
html code

<body>
<p>Hi this is a page.</p>
<p><button onclick='alert("LOL")'>LOL</button></p>
<p><a href='javascript:void(alert("Ow!"))'>Don't touch!</a></p>
</body>


javascript code

document.onclick = function(){
color = Math.floor(Math.random()*0xffffff).toString(16);
while(color.length < 6)
color = "0" + color;
document.body.style.backgroundColor = "#" + color;
};

 
 
 
2015 Sep 28 at 20:55 UTC — Ed. 2015 Sep 28 at 20:57 UTC
Page [1]