Skip to content

Commit 49e0411

Browse files
committed
Add functionality for copy the code
1 parent 0a32fa2 commit 49e0411

File tree

1 file changed

+33
-0
lines changed
  • Source-Code/GradientBackgroundGenerator

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
document.addEventListener('DOMContentLoaded', () => {
2+
const gradient = document.getElementById('gradient');
3+
const color1 = document.querySelector('.color1');
4+
const color2 = document.querySelector('.color2');
5+
const cssBackground = document.getElementById('css-background');
6+
const copyBtn = document.getElementById('copy-btn');
7+
8+
const updateBackground = () => {
9+
const color1Value = color1.value;
10+
const color2Value = color2.value;
11+
const background = `linear-gradient(to right, ${color1Value}, ${color2Value})`;
12+
13+
gradient.style.background = background;
14+
cssBackground.textContent = `background: ${background};`;
15+
};
16+
17+
const copyToClipboard = () => {
18+
const textToCopy = cssBackground.textContent;
19+
navigator.clipboard.writeText(textToCopy).then(
20+
() => {
21+
alert('CSS background value copied to clipboard!');
22+
},
23+
(err) => {
24+
console.error('Failed to copy: ', err);
25+
},
26+
);
27+
};
28+
color1.addEventListener('input', updateBackground);
29+
color2.addEventListener('input', updateBackground);
30+
copyBtn.addEventListener('click', copyToClipboard);
31+
// Initialize background on page load
32+
updateBackground();
33+
});

0 commit comments

Comments
 (0)