-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexercise5.js
38 lines (32 loc) · 1.11 KB
/
exercise5.js
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
/**
* Exercise #5: jQuery color picker
*/
$(document).ready(function () {
// 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]);
}
}
}
// create colored tiles
for (var i = 0; i < colors.length; i++) {
var tile = $("<div></div>");
tile.addClass("choice");
tile.css("background-color", colors[i]);
// we store the hex color value in a separate attribute as well
// (the background-color css property could also be used, but that returns
// colors in rbg(R,G,B) format instead of #RRGGBB)
tile.attr("hexcode", colors[i]);
$("#colors").append(tile);
}
// when a tile is clicked
$(".choice").click(function () {
var color = $(this).attr("hexcode");
$("#selected").html(color);
$("#selected").css("background-color", color);
});
});