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

145
Visualizações
JQuery each issue - div wrapped twice

I'm trying to solve an issue that I'm facing with JQuery about each. I have a list of items (class .list) that I need to target and wrap in a div (class .wrap). It works if the list but when there is a second one the loop wrap twice the list in the new div.

The idea is that every time there is a class called list, the loop wrap that specific class in a div called wrap.

Here is the code

HTML

 <div class='parent'>
    <div class='child'>
      <div class='list'>
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </div>
    </div>
  </div>
  
   <div class='parent'>
    <div class='child'>
      <div class='list'>
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </div>
    </div>
  </div>

JQuery

jQuery(".parent").each(function () {
  jQuery(".list").wrap("<div class='wrap'></div>");
});

This is the result with the error. The class list is wrapped twice as you can see from the example below.

<div class="parent">
    <div class="child">
      <div class="wrap"><div class="wrap"><div class="list">
        <div>1</div>
        <div>2</div>
        <div>3</div>
      </div></div></div>
    </div>
  </div>

And this is what I'm trying to achieve.

<div class="parent">
    <div class="child">
      <div class="wrap">
          <div class="list">
          <div>1</div>
          <div>2</div>
          <div>3</div>
        </div>
      </div>
    </div>
  </div>

Thank you in advance!

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

0

You want to select the list in the parent, not select all lists

jQuery(this).find(".list").wrap()
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