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

165
Vistas
jQuery change order of children anchors after each div

I have a div with children elements that currently looks something like this

    <div>
    <div class="div-el"></div>
    <a href="#">Anchor</a>
    <div class="div-el"></div>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    <div class="div-el"></div>
    </div>

Here's what I am trying to achieve

    <div>
    <div class="div-el"></div>
    <a href="#">Anchor</a>
    <a class="empty">Anchor</a>
    <div class="div-el"></div>
    <a href="#">Anchor</a>
    <a href="#">Anchor</a>
    </div>

So I am trying to make it so that

  1. There's at least 2 anchor elements next to each child .div-el element.
  2. In case if there's only one <a> element before the beginning of another .div-el element, add a blank <a> element like <a class="empty"></a>

Edit: I have been on it for hours and one of the things I have tried is the following with failure:

$('div > a').each(function(){
    var nextPromo = $(this).next();
    var prevPromo = $(this).prev();
    if (nextPromo.attr('href')) {
        
    }
    else {
        if (prevPromo.attr('href')) {
            
        }
        else {
            $('<a class="space"></a>').insertBefore(nextPromo);
        }
        
    }
});

I don't mind if we have to do this through a completely different approach.

Edit 2: I have had somewhat of a partial success with my script. The problem occurs when there's more than 2 anchors. No matter what, I'd like there to only be not more than 2 anchors before each div. So if there's 3 <a> elements in a row, we can push the last one to come after the div instead so its always like how I want it to be.

about 4 years ago · Juan Pablo Isaza
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