First
Second...">

Jquery reorder delimiters

I have 3 divs who want to cancel the order on the finished order

<div id="block-1">First</div> <div id="block-2">Second</div> <div id="block-3">Third</div> 

How to do this in jquery?

+8
jquery html
source share
4 answers
 <div id="parent"> <div id="block-1">First</div> <div id="block-2">Second</div> <div id="block-3">Third</div> </div> 

And try this in jQuery

 $('#parent > div').each(function() { $(this).prependTo(this.parentNode); });​ 

You can see an example in jsfiddle http://jsfiddle.net/N7PGW/

+19
source share

Just use

 $('#block-2').insertBefore('#block-1'); $('#block-3').insertBefore('#block-2'); 

Sample script: http://jsfiddle.net/2DUXF/

+10
source share

This will cancel all divs inside the div with id "div1"

  $(function(){ var items=$("#div1 div").toArray(); items.reverse(); $.each(items,function(){ $("#div1").append(this); }); });​ 

Here is jsFiddle http://jsfiddle.net/bCAVz/8/

+3
source share
 $( $("div[id|=block]") .slice(1) .get() .reverse() ) .insertBefore("div[id|=block]:first"); 

http://jsfiddle.net/8adwS/

Also note that you can add inverse array syntax to the jQuery function syntax, which will save you a selector.

0
source share

All Articles