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

131
Visualizações
How to use Javascript for loop to create buttons and call an on click function

I have been able to create the buttons using the for loop, but on click on a button, it picks the last value in the list of arrays, Why?

//list of propertySidebarItem styles to use to create the buttons
for (var i = 0; i < propertySidebarItem.styles.length; i++) {
  //current property style to use
  var style = propertySidebarItem.styles[i];
  //create the button using the current style
  var btnElement = document.createElement('button');
  btnElement.className = 'btn ' + element.tagName + style.name;
  //on click of any button, display the button properties
  btnElement.onclick = function () {
    //at this stage i am getting the last value in the propertySidebarItem.styles array
    console.log(btnElement);
  };
}
about 4 years ago · Juan Pablo Isaza
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