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

179
Views
Error de consola que dice que la variable no está definida, aunque está definida en la misma función

Tengo una variable llamada grid que contiene variables llamadas elementos.

Al hacer clic en un botón, me gustaría eliminar la cuadrícula y reemplazarla por una nueva. Esto se hace a través de un detector de eventos, una función y una instrucción if.

Sin embargo, recibo un error de consola que me dice "la cuadrícula no está definida".

¿Alguien puede ayudar?

 //Create preset variables let body = document.querySelector('body'); let content = document.querySelector('.content'); let numberOfSquares = 16; let randomColorValue = ''; let colorShade = 10; let buttonClicks = 0; /* let grid = document.createElement('div'); */ //Create variable called container that contains a div let container = document.createElement('div'); container.classList.add('container'); body.appendChild(container); // Add a button & position to top of screen let button = document.createElement('button'); button.textContent = "Refresh!"; button.classList.add('refresh-button'); content.appendChild(button); button.addEventListener('click', refreshGrid); squaresInGrid(); function squaresInGrid() { if (buttonClicks > 0) { console.log(buttonClicks); container.removeChild(grid); } for (i = 0; i < numberOfSquares; ++i) { let grid = document.createElement('div'); grid.classList.add('grid'); container.appendChild(grid); let element = document.createElement('div'); element.classList.add('grid-item'); element.addEventListener('mouseenter', function mouseEnterFunctions() { randomColor(); hoverStyle(); --colorShade; element.removeEventListener('mouseenter', mouseEnterFunctions); }); function hoverStyle() { element.style.backgroundColor = '#'+randomColorValue; element.style.opacity = (colorShade / 10); } function randomColor() { randomColorValue = Math.floor(Math.random()*16777215).toString(16); } grid.appendChild(element); } } //Function to be called on button click removing existing grid and replacing with a new one function refreshGrid() { ++buttonClicks; numberOfSquares = prompt("How many squares would you like? (Maximum 100)"); colorShade = 0; squaresInGrid(); }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Definió su variable de grid en el ciclo for, por lo que no se puede acceder a ella en ningún otro lugar que no sea dentro del ciclo.

En su lugar, debe descomentar su línea en la parte superior de su código para convertirla en una variable global. Luego, en el bucle for, elimine la palabra clave let delante de su variable for grid para que siempre haga referencia a la variable externa.

about 4 years ago · Juan Pablo Isaza 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!