Wednesday, December 28, 2011

jquery reverse order of children

Reverse the order of children:

(function($){
 $.fn.reverse = function(){  
  return this.each(function(){
   var i,
    $this = $(this),
    children = $this.children(),
    last_index = children.length - 1,
    last_child = $(children[last_index]);
   
   for (i = 0; i < last_index; ++i)
   {
    last_child.after($(children[i]).detach());
   };
  });
 };
})(jQuery);

No comments:

Post a Comment