Skip to content

Instantly share code, notes, and snippets.

@redrambles
Last active August 25, 2017 00:28
Show Gist options
  • Save redrambles/a76b737165949ab03a3c527e0faa87dc to your computer and use it in GitHub Desktop.
Save redrambles/a76b737165949ab03a3c527e0faa87dc to your computer and use it in GitHub Desktop.
JS BinRandom Background Color Changer// source https://jsbin.com/dacuyoc
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Random Background Color Changer">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body id="body">
<h1>Welcome to 102 Office Hours!</h1>
<p>Today we are going to build a small project to help us learn more about:</p>
<ul>
<li>Math.random()</li>
<li>Manipulating the DOM</li>
<li>Event Listeners</li>
<li>Building something from SCRATCH!!!</li>
</ul>
<button id="clicker">Change color!</button>
<p>Random background color: <span id="message">Click button to find out!</span></p>
<script id="jsbin-javascript">
//Change the background to a random color
// get the body element
// generate a random color
// set random color to body element's background color property
// make it work with a click!
var body = document.getElementById("body");
var clicker = document.getElementById("clicker");
var message = document.getElementById("message");
var changeColor = function(){
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
var color = "rgb("+r+","+g+","+b+")";
body.style.backgroundColor = color;
message.innerHTML = color;
};
clicker.addEventListener("click", changeColor);
</script>
</body>
</html>
//Change the background to a random color
// get the body element
// generate a random color
// set random color to body element's background color property
// make it work with a click!
var body = document.getElementById("body");
var clicker = document.getElementById("clicker");
var message = document.getElementById("message");
var changeColor = function(){
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
var color = "rgb("+r+","+g+","+b+")";
body.style.backgroundColor = color;
message.innerHTML = color;
};
clicker.addEventListener("click", changeColor);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment