I have a set of grid boxes that when i switch color pallet to a particular color, on mouse hover it should change to that color and when i click the rgb button the random color should override the color pallet that was first initialized.
But rbg button is displaying null because its outside of drawGrid() function
So the issue I am having is how to make both work at a different interval, whenever rbg button is clicked it should override the color pallet and vice versa.
function drawGrid(container, col, row) {
const box = document.createElement('div');
box.className = 'box';
box.id = `box${col}${row}`;
// Wehn color pallet is clicked mouseenter radom colors and it should ignore rbg button
const bgColor = document.querySelector('#bg-color');
box.addEventListener('mouseenter', () => {
box.style.backgroundColor = bgColor.value;
});
container.appendChild(box);
return (box);
}
function createBox(container) {
const grid = document.createElement('div');
grid.className = 'grid';
for(let i = 0; i < 6; i++) {
for(let j = 0; j < 5; j++) {
drawGrid(grid, i, j);
}
}
container.appendChild(grid);
}
function startupGrid() {
const game = document.querySelector('#game');
createBox(game)
}
startupGrid()
// Wehn rbg button is clicked mouseenter random colors and it should ignore color pallet
const rgb = document.querySelector('#rgb');
rgb.addEventListener('mouseenter', () => {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
let switchToRgb = `rgb ${r},${g},${b}`;
box.style.backgroundColor = switchToRgb;
})
body {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.main {
display: flex;
justify-content: center;
align-items: center;
place-items: center;
gap: 2rem;
margin: 5rem;
}
.controls {
display: grid;
gap: 1rem;
}
button {
padding: 5px 15px;
}
#game {
display: grid;
place-items: center;
}
.box {
width: 40px;
height: 30px;
border: 1px solid;
}
.grid {
display: grid;
grid-template-columns: repeat(6, auto);
grid-template-rows: repeat(5, auto);
box-sizing: border-box;
gap: 1px;
background: rgb(255, 255, 255)
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Grid</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="main">
<div class="controls">
<input type="color" id="bg-color">
<button id="rgb">RBG</button>
</div>
<div id="game"></div>
</div>
<script src="script.js"></script>
</body>
</html>
whereas if i add box to the addEventlistener inside drawGrid() function instead or rgb it works, but then the color pallet will stop working.
box.addEventListener('mouseenter', () => {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
let switchToRgb = `rgb ${r},${g},${b}`;
box.style.backgroundColor = switchToRgb;
})


rbgnotrgbas your code is expectingrgbis a CSS function and has it's parameters surrounded by parentheses i.e.rgb(${r}, ${g}, ${b})- link to the docsdrawGrid()function, so i tried adding it inside the function and i didn't receive any error, but the button is not working, is there a way I can calldrawGrid()inside theaddEventListenerso that when the button is clicked the random color should display on mousehover