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

230
Vistas
Insert element before other elements in the same div using JS

I want to add divs by using JavaScript in the parentDiv. I have already created some divs in my index.html file. I want that the div I will create will be added before other divs I created in html in the same parentDiv. I don't want to remove other divs. I tried using appendChild but that adds in the end. Hope it makes sense.

function displayBox(data) {
  let html = "";

  data.forEach((box) => {
  const box = document.createElement("div");
  box.classList.add("box");
    html = `
      <a
        target="_blank"
        href="${box.link}"
        class="image fit"
        ><img
            src="${box.img}"
            alt="${box.name}"
        /></a>
        <div class="inner">
        <h3>${box.name} (${box.country})</h3>
        <a
            target="_blank"
            href="${box.link}"
            class="button open-link fit"
            >Visit</a
        >
        </div>
      `;
    box.innerHTML = html;
    thumbnails.appendChild(box);
  });
  
}

thumbnails is my parentDiv where I want to add divs named box

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you want to add elements to the front you can use prepend.

var data = [1,2,3];

var out = document.querySelector("#out");
data.forEach(function (x) {
  var div = document.createElement("div");
  div.textContent = x;
  out.prepend(div);
});
<div id="out">

  <div>Orginal</div>
</div>

If you want to not be in revser order, you can either loop in reverse or change the code to use before() with the first element in the parent.

var data = [1,2,3];

var out = document.querySelector("#out");
const first = out.children[0];
data.forEach(function (x) {
  var div = document.createElement("div");
  div.textContent = x;
  first.before(div);
});
<div id="out">

  <div>Orginal</div>
</div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

It solved the problem in this way. Thanks epascarello

function displayBox(data) {
  data = data.reverse();
  let html = "";

  data.forEach((box) => {
  const box = document.createElement("div");
  box.classList.add("box");
    html = `
      <a
        target="_blank"
        href="${box.link}"
        class="image fit"
        ><img
            src="${box.img}"
            alt="${box.name}"
        /></a>
        <div class="inner">
        <h3>${box.name} (${box.country})</h3>
        <a
            target="_blank"
            href="${box.link}"
            class="button open-link fit"
            >Visit</a
        >
        </div>
      `;
    box.innerHTML = html;
    thumbnails.prepend(box);
  });
  
}
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