I am doing Etch a sketch project with JavaScript and DOM methods. This is what I did
- created a grid.
ChangeColour()
function called using onclick attribute.( Function includes EventListener( Event: mouseover) to give background colour for each cells.)
I want to stop Coloring cells when clicks on any Cell.
I tried return; and removeEventListner() function. Both didn't work.
HTML:
<section id="container"></section>
Javascript:
<script>
let i = 0;
container.innerHTML =
`<section class="row">${'<div class="cell" onclick="changeColour()"></div>'.repeat(n)}</section>`
.repeat(n)
function changeColour(){
const cells = document.querySelectorAll('div');
cells.forEach((div) => {
div.addEventListener('mouseover', () => {
div.setAttribute('style', 'background: black;');
})
})
}
</script>