I have code like:
<div class="a">Main</div>
<div class="b">1</div>
<div class="b">2</div>
<div class="b">3</div>
<div class="a">Another</div>
<div class="b">4</div>
<div class="b">5</div>
And I want the output to be:
<div class="a">Main</div>
<div class="b">3</div>
<div class="b">2</div>
<div class="b">1</div>
<div class="a">Another</div>
<div class="b">5</div>
<div class="b">4</div>
Am trying to use the following but it does not work correctly:
$.fn.reverseOrder = function() {
return this.each(function() {
$(this).prependTo( $(this).parent() );
});
};
$('.b').reverseOrder();
Because it reverses all the b divs to the top. Am a little lost. Any idea how to achieve this?
I do not wish to modify the code to add more divs inside it to contain them as it breaks other code of mine (not provided here).
I think I have to use nextAll and nextUntil functions.
First find last
divwithclass“a”. Then move the currentdiv.Also see my jsfiddle.
=== UPDATE ===
Here an alternative:
Also see my next jsfiddle.