Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!