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

237
Views
how to load function into div? js

Basically I need to load dynamically buttons into div and display them.

this is the code I have so far. The problem is that I need to display it inside of div, because I need to show them in this way (like in the picture). I tried with onload but apparently it doesn't work with div (<div id="from" onload="printBtn()">) when i tried with iframe, the buttons shows in the wrong place. how can i fix it?

pic

var uniqfrom = ["Tel-Aviv", "Amsterdam", "New York", "London"];

function printBtn() {
  for (var i = 0; i < uniqfrom.length; i++) {
    var btn = document.createElement("button");
    var t = document.createTextNode(uniqfrom[i]);
    btn.appendChild(t);
    document.body.appendChild(btn);
  }
}
body,
div {
  margin: 0px;
  padding: 0px;
}

#container {
  width: 100%;
  height: 500px;
}

#to,
#controls,
#from {
  float: left;
  width: 30%;
  height: 100%
}

#from {
  background-color: yellow;
}

#to {
  background-color: orange;
}

button {
  width: 200px;
  height: 50px;
  font-size: 20px;
  margin: 0 auto;
  background-color: blue;
  display: block;
  color: white;
}

h1 {
  text-align: center;
}

.sel {
  color: red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title></title>
</head>

<body>
  <div id="container">
    <div id="from">
    </div>
    <div id="to">
    </div>
  </div>
</body>

</html>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Get the DIVs that you want to put the buttons into, and append the buttons there instead of document.body.

var uniqfrom = ["Tel-Aviv", "Amsterdam", "New York", "London"];

function printBtn() {
  let from = document.getElementById("from");
  let to = document.getElementById("to");
  for (var i = 0; i < uniqfrom.length; i++) {
    var btn = document.createElement("button");
    var t = document.createTextNode(uniqfrom[i]);
    btn.appendChild(t);
    from.appendChild(btn);
    if (i > 0) {
      btn = btn.cloneNode(true);
      to.appendChild(btn);
    }
  }
}

printBtn();
body,
div {
  margin: 0px;
  padding: 0px;
}

#container {
  width: 100%;
  height: 500px;
}

#to,
#controls,
#from {
  float: left;
  width: 30%;
  height: 100%
}

#from {
  background-color: yellow;
}

#to {
  background-color: orange;
}

button {
  width: 200px;
  height: 50px;
  font-size: 20px;
  margin: 0 auto;
  background-color: blue;
  display: block;
  color: white;
}

h1 {
  text-align: center;
}

.sel {
  color: red;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title></title>
</head>

<body>
  <div id="container">
    <div id="from">
    </div>
    <div id="to">
    </div>
  </div>
</body>

</html>

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!