Home > OS >  How to group DIVs to one DIV using jQuery?
How to group DIVs to one DIV using jQuery?

Time:06-10

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

<div >
    <div>1</div>
</div>
<div >
    <div>2</div>
</div>
<div >
    <div>3</div>
</div>

and I need it looks like:

<div >
  <div >
    <div>1</div>
  </div>
  <div >
    <div>2</div>
  </div>
  <div >
    <div>3</div>
  </div>
</div>

I can't change HTML, only can use JS

CodePudding user response:

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 >
    <div>1</div>
</div>
<div >
    <div>2</div>
</div>
<div >
    <div>3</div>
</div>

CodePudding user response:

$( ".inside-div-class" ).wrap('');

$("div").wrapAll('')

  • Related