Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

181
Views
How to stop working of a function with EventListner initiated by HTML attribute 'onclick' on a grid when Clicks on any cell of Grid created using div

I am doing Etch a sketch project with JavaScript and DOM methods. This is what I did👇

  1. created a grid.
  2. 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>
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can't remove the event handler created from the onclick attribute because you don't have a reference to it - it was created by the HTML parser.

If you add the event listener in JavaScript instead of HTML, using changeColor as the handler, you should be able to remove it easily. I'm not sure this is exactly what you want to achieve, but along the lines of:

"use strict";
const n=3; // testing

container.innerHTML =
    `<section class="row">${'<div class="cell"></div>'.repeat(n)}</section>`
    .repeat(n)

const changeColor = event => {event.currentTarget.style.background="black"}
const clickHandler = event => {
  document.querySelectorAll('.cell').forEach( cell=> {
    cell.removeEventListener( "click", clickHandler);
    cell.removeEventListener("mouseover", changeColor);
  });
}


document.querySelectorAll('.cell').forEach(div=> {
     div.addEventListener("click",clickHandler);
     div.addEventListener("mouseover", changeColor);
});
.cell {
  display: inline-block;
  border: thin solid blue;
  width: 2rem;
  height: 2rem;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin></script>

<!-- body-html -->
<section id="container"></section>

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!