html
html
1
2
3
4
5
6
7
8
9
10
11
12
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div id="Cont" align="center">
<button id="MyButton">Generate Random Color</button>
<div align="center" id="MyColor"><h4 id="MyColorText"></h4></div>
</div>
</body>
</html>
Enter to Rename, Shift+Enter to Preview
css
css
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#MyColor {
height:4cm;
width:4cm;
border:2px solid black;
display:table-cell;
vertical-align:middle;
}
#MyButton {
margin-bottom:1cm;
padding:10px;
border:none;
background-color:green;
border-radius:10px;
cursor:pointer;
}
#MyColorText {
background-color:grey;
border-radius:100px;
padding:5px;
width:3cm
}
#MyColorText:empty {
display:none;
}
Enter to Rename, Shift+Enter to Preview
js
js
1
2
3
4
5
6
7
8
9
10
11
window.onload = function Events() {
document.getElementById("MyButton").addEventListener("click", GetColor);
};
function GetColor() {
var num = "#"+Math.floor(Math.random()*16777216).toString(16);
document.getElementById("MyColorText").innerText = num.toUpperCase();
document.getElementById("MyColor").style.backgroundColor = num;
}
Enter to Rename, Shift+Enter to Preview
BROWSER
Console
Run