tags:

views:

42

answers:

3

Hi

I'm very newbie with jQuery (and Javascript) and i don't know how to do this.

I want to grab two divs and insert them on 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>

Thank you very much

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

should do the trick, but untested.

Kind Regards

--Andy

jAndy
I just said to myself.. "why doing it easy if there is a hard way?".. :p so, I guess tvanfossons answer is best for that.
jAndy
+1  A: 

Well you could use "wrapAll"

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

(edited because I'm feeble-minded sometimes)

Pointy
wrap is the preferred way, yes
Aaron Mc Adam
I love how jQuery is so terse.
Christopher Altman
Except that this will wrap each element individually, not as a group.
tvanfosson
@tv arrgg you're right as usual!!! Hold on then.
Pointy
+4  A: 

Use wrapAll.

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