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

157
Vistas
Making HTML buttons from an array of names, and inserting them into the DOM

I can't figure out how to make these into buttons using the array:

var widgetButtons = ["Zoom In", "Zoom Out", "Pan", "Search"]

Any help would be appreciated. This is my code I am attempting to use

   <body>
   <div id="demo"></div>
   <script>
        var widgetButtons = ["Zoom In", "Zoom Out", "Pan", 
        "Search"];
   </script>
   </body>
   </html>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

In your HTML: <div id="new">

In your JS / script:

var widgetButtons = ["Zoom In", "Zoom Out", "Pan",
      "Search"];
    var element = document.getElementById("new");

    for (const widgetButtonsKey of widgetButtons) {
      var x = document.createElement("BUTTON");
      x.innerText = widgetButtonsKey
      console.log(x);
      element.appendChild(x);
    }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Here is another way to do it, using forEach().

IDs cannot contain spaces, so we remove the space from the item's name/text before setting the id.

const widgetButtons = ["Zoom In", "Zoom Out", "Pan", "Search"]
const demo = document.getElementById('demo');

widgetButtons.forEach( (item, idx) => {
  const btn = document.createElement('button');
  btn.id = item.replace(' ', '');
  btn.innerText = item;
  demo.appendChild(btn);
});
<html>
  <body>
    <div id="demo"></div>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's another approach.

var widgetButtons = ["Zoom In", "Zoom Out", "Pan", "Search"];
document.getElementById('demo').innerHTML = widgetButtons.map(text=>{
  return `<button>${text}</button>`;
}).join(' ');
<div id=demo></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