Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

122
Visualizações
Swapping divs from each function gets duplicated twice jQuery

I want to swap 2 divs using jquery but when I tried to swap it using insertAfter() in gets inserted twice. Just want to specify that each parent element needs to only swap the divs once.

So in each parent element wcmsd-step-container I need to insert the wcmsd-checkbox-item-description class below the wcmsd-step-title class. But it gets duplicated twice when swapping it.

Can anyone point what's the right function to use on my jQuery code?

jQuery(document).ready(function() {
  jQuery('.wcmsd-step-container').each(function() {
    jQuery('.wcmsd-checkbox-item-description').insertAfter(jQuery('.wcmsd-step-title'));
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!--Step 1-->
<div class="wcmsd-step-container">

  <h2 class="wcmsd-step-title">What type of photo do you want?</h2>

  <div class="wcmsd-step-content">
    <label class="wcmsd-checkbox-item-title">choose your photo</label>
    <p class="wcmsd-checkbox-item-description">Select up to 122. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
  </div>

</div>

<!--Step 2-->
<div class="wcmsd-step-container">

  <h2 class="wcmsd-step-title">What type of video do you want?</h2>

  <div class="wcmsd-step-content">
    <label class="wcmsd-checkbox-item-title">choose your video</label>
    <p class="wcmsd-checkbox-item-description">Select up to 322. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Your code inside the loop has no relation to the loop, so it's just for (i=0;i<2;++i) with the inner code saying "take all descriptions and put them all after every title" - you get two because there's two, if you had 3, you'd get 3 after each title.

You need to use this inside the loop to ensure the correct elements are moving to the correct place.

  $('.wcmsd-step-container').each(function() {
    $(this).find('.wcmsd-checkbox-item-description').insertAfter($(this).find('.wcmsd-step-title'));
  });

Updated snippet with a button to see before/after

$("#btn").click(() => {
  $('.wcmsd-step-container').each(function() {
    $(this).find('.wcmsd-checkbox-item-description').insertAfter($(this).find('.wcmsd-step-title'));
  });
});
.wcmsd-step-content { background-color: yellow; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="btn">click me</button>

<!--Step 1-->
<div class="wcmsd-step-container">

  <h2 class="wcmsd-step-title">What type of photo do you want?</h2>

  <div class="wcmsd-step-content">
    <label class="wcmsd-checkbox-item-title">choose your photo</label>
    <p class="wcmsd-checkbox-item-description">Select up to 122. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
  </div>

</div>

<!--Step 2-->
<div class="wcmsd-step-container">

  <h2 class="wcmsd-step-title">What type of video do you want?</h2>

  <div class="wcmsd-step-content">
    <label class="wcmsd-checkbox-item-title">choose your video</label>
    <p class="wcmsd-checkbox-item-description">Select up to 322. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi accumsan faucibus proin nisi, risus vehicula. Fames metus, metus consectetur.</p>
  </div>

</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda