-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
38 lines (30 loc) · 971 Bytes
/
script.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
const charset = "0123456789abcdef";
const color = document.querySelector(".js-color-name");
const image = document.querySelector(".js-image-container");
hexColor = "#";
document.querySelector(".js-button").addEventListener("click", () => {
let hex = "#";
for (let i = 0; i < 6; i++) {
hex += charset.charAt(randomNumber());
}
document.querySelector(".js-main").style.backgroundColor = hex;
color.textContent = hex;
color.style.color = hex;
hexColor = hex;
image.innerHTML = '<img src="./copy-icon.svg">';
image.title = "copy the hex code";
});
image.addEventListener("click", async () => {
image.innerHTML = '<img src="./checkmark-icon.svg">';
image.title = "copied";
try {
await navigator.clipboard.writeText(
hexColor === "#" ? "#000000" : hexColor
);
} catch (err) {
console.log(err);
}
});
function randomNumber() {
return Math.floor(Math.random() * charset.length);
}