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

196
Vistas
Insert each div inside next div (jQuery)

How can I insert div.one into his next div.two with jquery?

What it is:

<div id="list">
 <div class="box one"></div>
 <div class="box two"></div>
 <div class="box one"></div>
 <div class="box two"></div>
 <div class="box one"></div>
 <div class="box two"></div>
</div>

What I want:

<div id="list">
  <div class="box two">
    <div class="box one"></div>
  </div>
  <div class="box two">
    <div class="box one"></div>
  </div>
  <div class="box two">
    <div class="box one"></div>
  </div>
</div>

What I tried (but it insert all div.one in every div.two):

$("#list .box.one").each(function() {
    $(this).prependTo(".box.two").next();
});

Whats my fail?

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

0

You were close:

$("#list .box.one").each(function() {
    $(this).prependTo($(this).next());
});

using the class in the preprendTo() function will of course select all the .box.two elements, instead by doing $(this).next() we only get the very next sibling element.

about 4 years ago · Juan Pablo Isaza Denunciar

0

This should do what you want:

$("#list .box.one").each(function (index) {
    $(this).prependTo($("#list .box.two")[index]);
});
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