-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
27 lines (22 loc) · 918 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
document.addEventListener("DOMContentLoaded", () => {
const generateBtn = document.getElementById("generateBtn");
const colorBoxes = document.querySelectorAll(".color-box");
function generateColors() {
colorBoxes.forEach((box) => {
const randomColor = `#${Math.floor(Math.random() * 16777215).toString(16).padStart(6, "0")}`;
box.style.backgroundColor = randomColor;
box.textContent = randomColor.toUpperCase();
});
}
function copyToClipboard(event) {
const color = event.target.textContent;
navigator.clipboard.writeText(color).then(() => {
alert(`Copied ${color} to clipboard!`);
});
}
generateBtn.addEventListener("click", generateColors);
colorBoxes.forEach((box) => {
box.addEventListener("click", copyToClipboard);
});
generateColors();
});