I am building a sketch and I am stuck on making the relevant div.block
elements (in .gridcontainer
) to change on mouseover. I can achieve this if I change the query selector to "div"
instead of ".block"
, however this is not what I want.
Likely I need to change the logic completely. How should I achieve this?
let colours = document.querySelector(".block");
colours.addEventListener("mouseover", () => {
event.target.style.backgroundColor = "green";
});
let newGrid = document.querySelector('button');
newGrid.addEventListener("click", event => {
if (event.target.nodeName == "BUTTON") {
deleteChild();
let userInput = howMany();
let input = userInput * userInput;
let pixel = 500 / userInput;
for (let i = 0; i < input; i++) {
let i = addGrid(pixel)
}
}
});
function howMany() {
let userInput = prompt("how many?");
return userInput
}
function addGrid(pixel) {
let container = document.querySelector(".gridcontainer");
let grid = document.createElement('div');
grid.classList = ('block');
grid.textContent = 'hi';
grid.style.height = pixel + 'px';
grid.style.width = pixel + 'px';
container.appendChild(grid);
}
function deleteChild() {
let e = document.querySelector(".gridcontainer");
let child = e.lastElementChild;
while (child) {
e.removeChild(child);
child = e.lastElementChild;
}
}
.gridcontainer {
display: flex;
width: 500px;
height: 500px;
flex-direction: row;
gap: 0x;
flex-wrap: wrap;
}
.block {
display: flex;
flex-direction: column;
text-align: center;
color: blue;
}
<button>Change Canvas</button>
<div class=gridcontainer>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
<div class="block" style="height: 31.25px; width: 31.25px;">hi</div>
</div>