Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!