-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (67 loc) · 2.16 KB
/
main.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
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
const body = document.querySelector("body");
const button = document.getElementById("change-btn");
const instantButton = document.getElementById("instant-color");
const favbtn = document.getElementById("fav-btn");
const favList = document.getElementById("fav-list");
const liste = document.getElementById("liste");
const instantColorButton = document.getElementById("instant-color");
const colors = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"];
instantColorButton.addEventListener("click", copyText);
button.addEventListener("click", changeBackground);
favbtn.addEventListener("click", () => {
addFav(instantButton.textContent);
});
function changeBackground() {
let selectedId = "#";
do {
let randomColor = Math.floor(Math.random() * colors.length);
selectedId += colors[randomColor];
} while (selectedId.length <= 6);
body.style.backgroundColor = selectedId;
liste.style.backgroundColor = selectedId;
instantButton.innerText = selectedId;
}
function addFav(hexCode) {
let favItem = `
<ul style = 'background-color: ${hexCode}'>
<li>
<span>Color Code: <span>${hexCode}</span></span>
<span style='background-color:${hexCode}'></span>
</li>
</ul>
`;
favList.insertAdjacentHTML("beforeend", favItem);
}
function selectText() {
var element = event.target;
var range;
if (document.selection) {
// IE
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
range = document.createRange();
range.selectNode(element);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
}
}
function copyText() {
selectText();
alert('Color code ' + event.target.innerText + ' copied in clipboard')
document.execCommand("copy");
}
$("#instant-color").click(function () {
$(".alert").addClass("show");
$(".alert").removeClass("hide");
$(".alert").addClass("showAlert");
setTimeout(function () {
$(".alert").removeClass("show");
$(".alert").addClass("hide");
}, 5000);
});
$(".close-btn").click(function () {
$(".alert").removeClass("show");
$(".alert").addClass("hide");
});