-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise8.html
More file actions
76 lines (66 loc) · 1.71 KB
/
exercise8.html
File metadata and controls
76 lines (66 loc) · 1.71 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise #8: Color picker</title>
<style>
#colors {
width: 576px;
height: 216px;
border: 1px solid black;
}
#selected {
margin-top: 5px;
padding: 5px;
text-align: center;
width: 120px;
height: 1em;
border: 1px solid black;
}
.choice {
float:left;
width: 20px;
height: 20px;
border: 1px solid black;
margin: 1px;
}
</style>
<script src="exercise8.js"></script>
</head>
<body>
<div id="colors"></div>
<div id="selected"></div>
<script>
function initGrid() {
// collect colors in an array
var colors = [];
var range = ["00", "33", "66", "99", "cc", "ff"];
for (var r = 0; r < range.length; r++) {
for (var g = 0; g < range.length; g++) {
for (var b = 0; b < range.length; b++) {
colors.push("#" + range[r] + range[g] + range[b]);
}
}
}
// creating colored tiles
var cdiv = document.getElementById("colors");
for (var i = 0; i < colors.length; i++) {
var tile = document.createElement("div");
tile.className = "choice";
tile.style.backgroundColor = colors[i];
tile.addEventListener("click", tileClicked);
cdiv.appendChild(tile);
}
}
function tileClicked() {
var color = this.style.backgroundColor;
var sel = document.getElementById("selected");
sel.style.backgroundColor = color;
sel.innerHTML = color;
}
window.onload = function () {
initGrid();
}
</script>
</body>
</html>