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

137
Visualizações
Bucle de matriz en el evento de botón al hacer clic

Tengo esta matriz y quiero hacer un bucle en los botones. La primera celda estará en el evento onclick, la segunda estará en innerHTML. Y agregue estos botones a div

La matriz:

 rows = [ [fun1(), 'innerhtml'], [fun2(), 'innerhtml'], [fun3(), 'innerhtml'], ];
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Esto hará que trabajes como esperas.

 const fun1 = () => alert("Hello function 1"); const fun2 = () => alert("Hello function 2"); const fun3 = () => alert("Hello function 3"); const rows = [ [fun1, 'innerhtml'], [fun2, 'innerhtml'], [fun3, 'innerhtml'], ]; const myDiv = document.getElementById('myDiv'); rows.forEach(row => { // Create button const button = document.createElement('button'); // Apply onclick event button.onclick = row[0]; button.innerHTML = row[1]; // Append to div myDiv.appendChild(button); }) 

 const fun1 = () => alert("Hello function 1"); const fun2 = () => alert("Hello function 2"); const fun3 = () => alert("Hello function 3"); const rows = [ [fun1, 'innerhtml'], [fun2, 'innerhtml'], [fun3, 'innerhtml'], ]; const myDiv = document.getElementById('myDiv'); rows.forEach(row => { // Create button const button = document.createElement('button'); // Apply onclick event button.onclick = row[0]; button.innerHTML = row[1]; // Append to div myDiv.appendChild(button); })
 <div id="myDiv"></div>

Los comentarios deben explicar lo que sucede en mi código.

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