I used Javascript to create a grid with a dynamic numbers of rows and columns. In addition, I use javascript to highlight the boxes which I hovered over. However, when I create a new grid with a different numbers of rows and columns, the style changes to highlighted boxes persist. How can I undo those changes? I.e. how can I start with a clear grid?
let btn = document.getElementById("start")
btn.addEventListener("click", createGrid)
function createGrid() {
let numberOfRows = prompt("How many rows would you like?");
let i = 0;
let x = numberOfRows**2;
document.documentElement.style.setProperty("--columns-row", numberOfRows);
for (i; i < x ; i++) {
var div = document.createElement("div");
document.getElementById("container").appendChild(div);
div.addEventListener("mouseenter", function () {
this.style.backgroundColor = "red";
});
}
}
:root {
--columns-row: 2;
}
#container {
display: grid;
grid-template-columns: repeat(var(--columns-row), 1fr);
grid-template-rows: repeat(var(--columns-row), 1fr);
width: 500px;
height: 500px;
}
div {
width: 100%;
height: 100%;
outline: 1px solid;
float: left;
background-color: white;
}
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title>Etch-a-sketch</title>
</head>
<body>
<h1>Etch-a-sketch</h1>
<button id="start">Start</button>
<div id="container"></div>
</body>