I am trying to build random color generator. It creates box for each generated color. You can hover over each of them and copy or lock the color you like.
The problem appears when I click on copy button or the background part. It also triggers lock button, and lock button also triggers copy button.
I have no idea why it's happening because there is e.stopPropagation)
in functions that are used.
I am new in js world so if you spot anything that can be fixed I'll be grateful for letting me know how it can be improved
FULL Code:
//selecting JS OBJECTS
const background = document.querySelector('.background');
const copy = document.querySelector('#copy');
const p = document.querySelector('p');
const body = document.querySelector('body');
const colorContainer = document.querySelector('.color-container');
let spacebarFirstCount = 0;
body.addEventListener('keydown', (e) => {
if (e.key === " ") {
const color = generateRGB();
changeBackgroundColor(color);
p.innerText = color;
if (copy.innerText != "copy") {
copy.innerText = "copy";
}
addColorBox(color);
if (spacebarFirstCount !== 3) {
if (spacebarFirstCount === 2) {
document.querySelector('.spacebar').remove();
}
spacebarFirstCount++;
}
}
});
copy.addEventListener('click', () => {
navigator.clipboard.writeText(p.innerText);
copy.innerText = "done";
})
const addColorBox = (color) => {
const colorBox = document.createElement('div');
colorBox.style.backgroundColor = color;
colorBox.classList.toggle('color-box');
colorBox.addEventListener('mouseover', hover2)
fixBoxNumber();
colorContainer.append(colorBox);
}
const generate255 = () => {
return Math.floor(Math.random() * 255) + 1;
}
const generateRGB = () => {
const r = generate255();
const g = generate255();
const b = generate255();
const alpha = (Math.floor((Math.random() + 0.1) * 100) / 100);
return `rgb(${r},${g},${b},${alpha})`;
}
const changeBackgroundColor = (color) => {
background.style.backgroundColor = color;
}
function hover2() {
const copyText = document.createElement('span');
const lockText = document.createElement('span');
copyText.append("copy");
copyText.classList.toggle("hoverText");
lockText.classList.toggle("hoverText");
this.classList.toggle('box-animation');
this.removeEventListener('mouseover', hover2);
const currentColor = background.style.background;
changeBackgroundColor(this.style.backgroundColor);
function copyToClipboard(event) {
navigator.clipboard.writeText(this.style.backgroundColor);
copyText.innerText = "copied";
this.removeEventListener('click', copyToClipboard);
event.stopPropagation();
}
function leaving() {
copyText.remove();
lockText.remove();
this.classList.remove('box-animation');
this.addEventListener('mouseover', hover2);
this.removeEventListener('click', copyToClipboard);
this.removeEventListener('mouseleave', leaving);
this.removeEventListener('click', locking);
}
this.addEventListener('click', copyToClipboard);
this.addEventListener('mouseleave', leaving)
if (!this.hasAttribute('locked')) {
lockText.append("lock");
}
else {
lockText.append("locked");
}
// ! Stop propagation nie działa
function locking(e) {
if (!this.hasAttribute('locked')) {
this.setAttribute('locked', 'true');
lockText.innerText = "locked";
} else {
this.removeAttribute('locked');
lockText.innerText = "lock";
}
e.stopPropagation();
}
this.addEventListener('click', locking);
this.append(lockText);
this.append(copyText);
}
const fixBoxNumber = () => {
if (colorContainer.childElementCount >= 19) {
const deleteBoxes = Object.values(colorContainer.childNodes).filter((box) => !box.hasAttribute("locked"));
if (deleteBoxes.length < 12) {
for (let i = 0; i < deleteBoxes.length; i++) {
deleteBoxes[i].remove();
}
}
else {
for (let i = 0; i < 12; i++) {
deleteBoxes[i].remove();
}
}
}
}