So basically I have functions that creates a grid. When I hover over a particular cell I want the cell to be filled black. But the fill should remain in the cell after the hover. How do I do that using JS?
I'm new to JS and this is my first time asking a programming related question so I apologize if my question is not clear enough.
const container = document.querySelector('#container');
let rows = document.getElementsByClassName('grid-row');
let cells = document.getElementsByClassName('grid-column');
defaultGrid();
function defaultGrid() {
createRow(18);
createColumn(18);
}
function createRow(rowNum) {
for (let i = 0; i < rowNum; i++) {
let row = document.createElement('div');
container.appendChild(row).className = 'grid-row';
}
}
function createColumn(cellNum) {
for (let i = 0; i < rows.length; i++) {
for (let j = 0; j < cellNum; j++) {
let newCell = document.createElement('div');
rows[j].appendChild(newCell).className = 'grid-column';
}
}
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.grid-column {
display: inline-block;
border: 1px solid black;
min-height: 20px;
min-width: 20px;
margin-left: 2px;
margin-top: 2px;
}
#container {
border: 5px solid chartreuse;
}
.fill {
background: black;
}
<h2>test</h2>
<div id="container">
</div>