(jQuery) How to grab two divs and insert them on a new div

I am very new with jQuery (and Javascript) and I have no idea how to do this.

I want to grab two divs and insert them into a new div.

Example:

 <div class="test"></div>
 <div class="test"></div>
 <div class="test2"></div>
 <div class="test2"></div>

      

to

<div class="float">
    <div class="test"></div>
    <div class="test"></div>
</div>

<div class="test2"></div>
<div class="test2"></div>

      

Many thanks

+2


a source to share


3 answers


Use wrapAll .



 $('div.test').wrapAll( "<div class='float'></div>" );

      

+4


a source


Well you can use "wrapAll"

$('div.test').wrapAll('<div class="float"/>');

      



( edited because sometimes I am weak-minded)

+1


a source


$('<div/>', {
  class:   'float',
  css:     {
     position: 'relative',
     display:  'block'
  }
}).append($('.test')).prependTo('body');

      

should do the trick, but not tested.

0


a source







All Articles