Skip to content

Instantly share code, notes, and snippets.

@Bijesse
Created January 30, 2015 14:44
Show Gist options
  • Save Bijesse/106c1d5c62adef7371bc to your computer and use it in GitHub Desktop.
Save Bijesse/106c1d5c62adef7371bc to your computer and use it in GitHub Desktop.
Color changing buttons Cristo Rey // source http://jsbin.com/jobeca
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Color changing buttons Cristo Rey</title>
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
font-size: xx-large;
font-weight: bold;
background-color: #E3F0FB;
border-radius: 4px;
padding: 10px;
text-align: center;
}
button {
margin-top: 10px;
border-radius: 4px;
border: thin solid #F0E020;
padding: 5px;
background-color: #F8F094;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-weight: bold;
color: #663300;
width: 75px;
}
button:hover {
border: thin solid #FFCC00;
background-color: #FCF9D6;
color: #996633;
cursor: pointer;
}
button:active {
border: thin solid #99CC00;
background-color: #F5FFD2;
color: #669900;
cursor: pointer;
}
</style>
</head>
<body>
<div id="mainContent">
<button id="randomColorButton">random color</button>
<button id="askColorButton" class="buttonStyle">pick a color</button>
</div>
<div id="colorButtonsContainer"></div>
<script id="jsbin-javascript">
var colors = ["cyan", "aliceblue", "darkmagenta", "gold", "azure", "green", "moccasin"];
var randomColorButton = document.getElementById("randomColorButton");
randomColorButton.addEventListener('click', changeToRandomColor, false);
function changeToRandomColor() {
var index = Math.floor((Math.random() * colors.length) + 1);
document.body.style.backgroundColor = colors[index];
}
var askColorButton = document.getElementById("askColorButton");
askColorButton.addEventListener('click', changeToUserSelectedColor, false);
function changeToUserSelectedColor() {
var color = prompt("What is your favorite color?");
document.body.style.backgroundColor = color;
}
var colorButtonsContainer = document.getElementById("colorButtonsContainer");
for (var i = 0; i < colors.length; i++) {
var newButton = document.createElement("button");
newButton.innerHTML = colors[i];
newButton.addEventListener('click', changeColor, false);
colorButtonsContainer.appendChild(newButton);
}
function changeColor() {
document.body.style.backgroundColor = this.innerHTML;
}
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<title>Color changing buttons Cristo Rey</title>
<style type="text/css">
body {
font-family: Arial, Helvetica, sans-serif;
font-size: xx-large;
font-weight: bold;
background-color: #E3F0FB;
border-radius: 4px;
padding: 10px;
text-align: center;
}
button {
margin-top: 10px;
border-radius: 4px;
border: thin solid #F0E020;
padding: 5px;
background-color: #F8F094;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
font-weight: bold;
color: #663300;
width: 75px;
}
button:hover {
border: thin solid #FFCC00;
background-color: #FCF9D6;
color: #996633;
cursor: pointer;
}
button:active {
border: thin solid #99CC00;
background-color: #F5FFD2;
color: #669900;
cursor: pointer;
}
</style>
</head>
<body>
<div id="mainContent">
<button id="randomColorButton">random color</button>
<button id="askColorButton" class="buttonStyle">pick a color</button>
</div>
<div id="colorButtonsContainer"></div>
</body>
</html>
</script>
<script id="jsbin-source-javascript" type="text/javascript">var colors = ["cyan", "aliceblue", "darkmagenta", "gold", "azure", "green", "moccasin"];
var randomColorButton = document.getElementById("randomColorButton");
randomColorButton.addEventListener('click', changeToRandomColor, false);
function changeToRandomColor() {
var index = Math.floor((Math.random() * colors.length) + 1);
document.body.style.backgroundColor = colors[index];
}
var askColorButton = document.getElementById("askColorButton");
askColorButton.addEventListener('click', changeToUserSelectedColor, false);
function changeToUserSelectedColor() {
var color = prompt("What is your favorite color?");
document.body.style.backgroundColor = color;
}
var colorButtonsContainer = document.getElementById("colorButtonsContainer");
for (var i = 0; i < colors.length; i++) {
var newButton = document.createElement("button");
newButton.innerHTML = colors[i];
newButton.addEventListener('click', changeColor, false);
colorButtonsContainer.appendChild(newButton);
}
function changeColor() {
document.body.style.backgroundColor = this.innerHTML;
}</script></body>
</html>
var colors = ["cyan", "aliceblue", "darkmagenta", "gold", "azure", "green", "moccasin"];
var randomColorButton = document.getElementById("randomColorButton");
randomColorButton.addEventListener('click', changeToRandomColor, false);
function changeToRandomColor() {
var index = Math.floor((Math.random() * colors.length) + 1);
document.body.style.backgroundColor = colors[index];
}
var askColorButton = document.getElementById("askColorButton");
askColorButton.addEventListener('click', changeToUserSelectedColor, false);
function changeToUserSelectedColor() {
var color = prompt("What is your favorite color?");
document.body.style.backgroundColor = color;
}
var colorButtonsContainer = document.getElementById("colorButtonsContainer");
for (var i = 0; i < colors.length; i++) {
var newButton = document.createElement("button");
newButton.innerHTML = colors[i];
newButton.addEventListener('click', changeColor, false);
colorButtonsContainer.appendChild(newButton);
}
function changeColor() {
document.body.style.backgroundColor = this.innerHTML;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment