I have a list:
<ul>
<li>milk</li>
<li>butter</li>
<li>eggs</li>
<li>orange juice</li>
<li>bananas</li>
</ul>
Using javascript how can I reorder the list items randomly?
I have a list:
<ul>
<li>milk</li>
<li>butter</li>
<li>eggs</li>
<li>orange juice</li>
<li>bananas</li>
</ul>
Using javascript how can I reorder the list items randomly?
Based no @Alexey Lebedev's answer, if you prefer a jQuery function that shuffles elements, you can use this one:
And then call it like this:
This is based on Fisher–Yates shuffle, and exploits the fact that when you append a node, it's moved from its old place.
Performance is within 10% of shuffling a detached copy even on huge lists (100 000 elements).
http://jsfiddle.net/qEM8B/
Simply put, like this:
JS:
HTML:
Demo: http://jsbin.com/itesir/edit#preview
Here is a very simple way to shuffle with JS:
http://www.w3schools.com/js/js_array_sort.asp