Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to group DIVs to one DIV using jQuery?

I have several DIVs with same class, and I need using jQuery or pure JS group it into one DIV

<div class="a">
    <div>1</div>
</div>
<div class="a">
    <div>2</div>
</div>
<div class="a">
    <div>3</div>
</div>

and I need it looks like:

<div class="new-class">
  <div class="a">
    <div>1</div>
  </div>
  <div class="a">
    <div>2</div>
  </div>
  <div class="a">
    <div>3</div>
  </div>
</div>

I can’t change HTML, only can use JS

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use wrapAll method

$(".a").wrapAll("<div class='new-class'></div>")
.new-class {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="a">
    <div>1</div>
</div>
<div class="a">
    <div>2</div>
</div>
<div class="a">
    <div>3</div>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading