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

240
Vistas
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 Respuestas
Responde la pregunta

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 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