Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
Referencing a yet to be created element in another element's event listener in JavaScript

I have an element created dynamically, for which I have already attached an event listener to, like below:

radio.addEventListener('click', (event) => {
   document.getElementById(action[1]).style.display = 'block';
});

The 'action[1]' is a string which will be the id of an element yet to be created. I have tried this, and it seems to work. I know the event listener's function will only be fired on the 'click' event, and by then the 'action[1]' element will exist. But it seems like I'm attaching an invalid function when the addEventListener is attached to my 'radio' element. Will this cause problems?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Okay, I want to close this with an answer, but the answer, comes from Barmar, so I don't take credit for it. As stated in the comment,

The body of a function isn't evaluated until the function is called. There's no problem with access variables that are assigned between adding the event listener and triggering the event.

Question is answered.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda