Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda