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

214
Views
Wrap sibling elements in a DIV with javascript

I want to turn the elements with class "a" into a div with javascript, without losing the order in which they are found with their siblings. Here is the code, I have not made much progress, I appreciate your help in advance.

const elementParent= document.querySelectorAll(".a");
console.log(elementParent)

const parent= elementParent[0].parentNode;
console.log(parent)

parent.innerHTML= elementParent[0]
<div style="display:flex">
  <div class="cx">0</div>
  <div class="cx">0</div>
  <div class="a">A</div>
  <div class="a">A</div>
  <div class="b">b</div>
</div>
The final result of the html should be:

   <div style="display:flex">
      <div class="cx">0</div>
      <div class="cx">0</div>
      <div>
        <div class="a">A</div>
        <div class="a">A</div>
      </div>
      <div class="b">b</div>
    </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is not an exact solution, but I think it helps

  1. create a div: document.createElement()
  2. fill its innerHTML from original element
  3. use insertbefore to insert new before the original
  4. use .remove() to remove the original.
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!