ssdesign
ssdesign

Reputation: 2821

How to move two divs upqwards in the dom using jquery?

I an not sure how to do this. I have the following code:

<div class="one">You might also invite...</div>
<div class="pic1"></div>
<div class="name1">Some name</div>
<div class="pic2"></div>
<div class="name2">Another Name</div>
<div class="pic3"></div>
<div class="name3">Third Name</div>

Now, what I want is, that one some event, I want the divs with class pic2 and name2 to jump to another position in the code, like this:

<div class="pic2"></div>
<div class="name2">Another Name</div>
<div class="one">You might also invite...</div>
<div class="pic1"></div>
<div class="name1">Some name</div>
<div class="pic3"></div>
<div class="name3">Third Name</div>

How should this be achieved? Should I be using something like "prepend"? or I would have to recurse through the dom and reposition them...

Thanks

Upvotes: 2

Views: 68

Answers (1)

Travis J
Travis J

Reputation: 82337

jsFiddle Demo

You would use insertBefore()jQuery API

$('.pic2').insertBefore('.one');
$('.name2').insertBefore('.one');

Upvotes: 3

Related Questions