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

189
Vistas
How would I add access different elements in this array that I pushed inside the for loop?
let body = document.querySelector("body");
let container = document.querySelector("#container");
//Accessing HTML Elements
let inputArray = [];
    
for (let element = 0; element < 30; element++) {
  space = document.createElement("input");
  container.appendChild(space);
  space.setAttribute("id", element);
  space.classList.add("square");
  inputArray.push(space); 
}
//For loop that creates a grid of squares and adds each square to an array 
    
document.onkeydown = event => {
  if (event.keyCode === 37) {
    for (let i = 0; i < 31; i++) {
      inputArray[i].value = ""; 
    }
  }
}
//When the left arrow key is pressed, the text in every square disappears
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I do not see the problem. (Apart from the error Uncaught TypeError: Cannot set properties of undefined (setting 'value') : you using loop i<31 to the clear, but you only use i<30 to the create).
And a little mistake: you do not define the space variable (I recommend to use let).

about 4 years ago · Juan Pablo Isaza Denunciar

0

If I understood your question correctly?

const container = document.querySelector('#container')
 
const inputArray = Array.from(({length:30}),(e,i)=>
  {
  let space = container.appendChild( document.createElement('input') )
  space.id          = 
  space.placeholder = i
  space.className   = 'square'
  })
    
container.onkeydown = evt =>
  {
  if (evt.key === 'ArrowLeft' && evt.target.matches('input'))
    {
    evt.target.value = ''; 
    console.clear()
    console.log(`element.id = "${ evt.target.id }"`)
    }
  }
.square {
  margin : .2em;
  width  : 6em;
  }
<div id="container"></div>

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