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

124
Vistas
How do I pass a function with argument into event Listener

In my drawing app I have a function called addMouseOver. It adds an eventlistener to all squares so that when you mouseover them they turn black. The problem is that it works on the initial grid but I also have a resize button with multiple click event listeners that will remove the current grid items, adds new ones and then adds the mouseover function also. I'm not sure why the function is not working in the event listener.

    // Get grid container as variable, create grid item so we can add using for loop
let gridContainer = document.querySelector('.grid-container');
let clearButton = document.querySelector('.clear');
let resizeButton = document.querySelector('.resize');
let squaresPerSide = 16;

// Grid container is set up to divide whole page 16x16 for loop will add that amount of items
function createGrid(container, size) {
  container.style.gridTemplateColumns = `repeat(${size}, 1fr)`;
  container.style.gridTemplateRows = `repeat(${size}, 1fr)`;
  for (let i = 0; i < size * size; i++) {
    let gridItem = document.createElement('div');
    gridItem.classList.add('grid-item');
    container.appendChild(gridItem);
  }};

// Get grid item to change color when hovered over
// and change back when mouse isn't over
function addMouseOver(gridItems) {
  gridItems.forEach(item => 
    item.addEventListener('mouseover', () =>
    item.style.backgroundColor = 'black' ))
  };

// function to reset color of blocks when clear button clicked
//  has to work for all grid sizes

createGrid(gridContainer, squaresPerSide);
let allGridItems = document.querySelectorAll('.grid-item');
addMouseOver(allGridItems);

clearButton.addEventListener('click', () =>
  allGridItems.forEach(item =>
    item.style.backgroundColor = 'rgb(177, 229, 231)'));

// Function to prompt user to select amount of squares per side 
// when resize button is clicked, and change grid accordingly.

resizeButton.addEventListener('click', () => 
  allGridItems.forEach(item => item.remove()));
resizeButton.addEventListener('click', () =>
  squaresPerSide = parseInt(prompt('How many squares would you like per side?')))
resizeButton.addEventListener('click', () =>
  createGrid(gridContainer, squaresPerSide));
resizeButton.addEventListener('click', () =>
  addMouseOver(allGridItems));
about 4 years ago · Juan Pablo Isaza
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