jQuery: Randomly reorder children elements of selected elements

Posted

I'm working on a new template for a client, and they asked me to display some list elements in a random order on each page load. I did my Google due diligence, but didn't come up with anything that wasn't slideshow-like -- what I wanted was just something that would show all of the children elements of an element, but in a random order. (In this particular case, the children elements were case case studies.) I was already using jQuery on the page, so I wrote a plugin: jquery.reorder.js (1k) Here's the code:

$.fn.reorder = function() {

  // random array sort from
  // http://javascript.about.com/library/blsort2.htm
  function randOrd() { return(Math.round(Math.random())-0.5); }

  return($(this).each(function() {
    var $this = $(this);
    var $children = $this.children();
    var childCount = $children.length;

    if (childCount > 1) {
      $children.remove();

      var indices = new Array();
      for (i=0;i

Thoughts? Suggestions? Let me know.

Posted

10 comments

Jan 08, 2008
rdmey | Cycle through list elements with jQuery said...
[...] Randomly reorder the list items before you add the rest of the code [...]
Jun 23, 2008
Rich Barrett-Small said...
This is just what I was looking for. Nicely done, thanks!
Sep 02, 2008
Steven Black said...
Hey, thank you very much for that. See it on http://K7Waterfront.org. Works great!

I found your blog while Googling jQuery terms related to this. Keep up the good work, and thanks for sharing, Rebecca!

Cheers!

May 07, 2009
Rachel Nabors said...
I remember when you posted this, and now I've stumbled over it googling for a solution to the same problem! Haha! Thanks for the plugin.
May 27, 2009
Henrik said...
perfect, just what i needed.

thank you.

Jul 11, 2009
Hlava bloguje » Blog Archive » Zmena poradia HTML elementov said...
[...] na to jQuery plugin reorder, ktorý náhodne zmení poradie všetkých elementov vybraného elementu. Čiže sa nemusí jednať [...]
Jul 24, 2009
george said...
Hi! Im new to Jquery/javascript.

is this correct to use your function?

$(document).ready(function(){
$('#item').reorder('', function() {
});
});

Nov 17, 2009
Karl Varga said...
This is great, thank you! The only problem for me was that I needed to keep the data attached to my child elements, and when you move an element, jQuery destroys the data (it doesn't even keep it when you deep clone it!).

So I've made some slight modifications. Because you can't introspect the data on an element, I have provided a callback which is passed the child element and its clone. In the callback you can copy over any data you wish.

In summary: the children are hidden; then in a loop each is cloned and the clone is appended to the container (and shown) and the callback is called; then the child is removed.

Here is the Gist: http://gist.github.com/236696

May 08, 2010
Kevin Leary said...
Thanks for this Rebecca, I've used it to randomly display quotes in the sidebar of my WordPress portfolio, and only display the first item using:


$('#quotes').reorder().find('blockquote:not(:first)').hide();

Jul 23, 2010
Michael said...
Hi Rebecca,
Could you also do a version of this where I can pass in a specific element order? My use case: I have a set of elements on the page and due to other users manipulation I want to sort the elements for his colleagues now the same way. I would send the element order from the server to the client, but which is the most efficient (and non-destructive) way to sort the existing elements in the submitted order?

Greetings from Munich!

Leave a comment...