I was recently looking for a decent way of shuffling a set of elements. I found a jQuery plugin which claimed to do exactly that but unfortunately it only works if all elements are direct descendants of a single parent, like all list items under an unordered list.
I needed something that would work with any elements, regardless of their position within the DOM. In the end I didn’t find a solution so I attempted it myself, and here’s the end result:
$.fn.shuffle
(function($){ $.fn.shuffle = function() { var allElems = this.get(), getRandom = function(max) { return Math.floor(Math.random() * max); }, shuffled = $.map(allElems, function(){ var random = getRandom(allElems.length), randEl = $(allElems[random]).clone(true)[0]; allElems.splice(random, 1); return randEl; }); this.each(function(i){ $(this).replaceWith($(shuffled[i])); }); return $(shuffled); }; })(jQuery); |
Usage
// Shuffle all list items within a list: $('ul#list li').shuffle(); // Shuffle all DIVs within the document: $('div').shuffle(); // Shuffle all <a>s and <em>s: $('a,em').shuffle(); |
This plugin can be used on any set of elements, obviously performance needs to be taken into account when shuffling many elements.
If you are worried about performance then you might prefer this pure JavaScript method (~10% faster):
// WARNING: I haven't yet tested this // - It's likely that it will strip events upon shuffling function shuffle(elems) { allElems = (function(){ var ret = [], l = elems.length; while (l--) { ret[ret.length] = elems[l]; } return ret; })(); var shuffled = (function(){ var l = allElems.length, ret = []; while (l--) { var random = Math.floor(Math.random() * allElems.length), randEl = allElems[random].cloneNode(true); allElems.splice(random, 1); ret[ret.length] = randEl; } return ret; })(), l = elems.length; while (l--) { elems[l].parentNode.insertBefore(shuffled[l], elems[l].nextSibling); elems[l].parentNode.removeChild(elems[l]); } } // Usage: shuffle( document.getElementsByTagName('li') ); |
Thanks for reading! Please share your thoughts with me on Twitter. Have a great day!
Works like a charm, thanks James!
Just used it to “randomize” the questions on an online quiz I was building for a client.
Thanks for your comment Chris, I’m glad you found it useful! 🙂
I have a set of questions that are located in a XML file. I need a php file to shuffle these questions and display them to the user.
Is there a way I can shuffle the questions in the XML DOM?
I need help with the syntax because I am fairly new to php programming?
Thank you!
I’m trying to make a matching game in JS, this is exactly what I was looking for. Thanks james!
Hi James,
The Shuffle is awesome…im using it showcase my portfolio…
i have seperate sections…website graphic 3dGraphic and Motion and All which are going to be the list that you choose from…
anywayz i was wondering if there was anyway that when the page loads up it shows website first, and then you have the option to view all in the list….
any help would be appreciated..thanx
The Shuffle is awesome…im using it showcase my portfolio…
i have seperate sections…website graphic 3dGraphic and Motion and All which are going to be the list that you choose from…
anywayz i was wondering if there was anyway that when the page loads up it shows website first, and then you have the option to view all in the list….
any help would be appreciated..thanx
just realised what i did, so heres the post again with my name on top 😉
is the any way to shuffle list ( ) parsed by $.ajax() from XML-file ?