Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To simply randomise the order of an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array" rel="nofollow"><em>Array</em></a>, you could do something like this</p> <pre><code>function shuffleArray(a) { // Fisher-Yates shuffle var i = a.length, t, j; while (--i) { // loop over each item in array (except 0 because no point) j = (Math.random() * (i+1)) | 0; // random Int j &lt;= i t = a[i], a[i] = a[j], a[j] = t; // swap items i and j } } </code></pre> <p>You'll need to convert your <a href="https://developer.mozilla.org/en/docs/DOM/HTMLCollection" rel="nofollow"><em>HTMLCollection</em></a> to something non-live so you can use it more easily, for example use</p> <pre><code>elements1 = document.getElementById('elements1'); // cache for later nodes1 = Array.prototype.slice.call(elements1.children); // children to Array elements2 = document.getElementById('elements2'); nodes2 = Array.prototype.slice.call(elements2.children); </code></pre> <p>Now for the thing you actually want to randomise (if you don't want to modify the function above to shuffle two arrays at the same time, see below)</p> <pre><code>i = nodes1.length, a = new Array(i); while (i--) a[i] = i; // a = [0, 1, 2, 3, ...] </code></pre> <p>Now simply randomise <code>a</code>, and re-append the nodes following the new order of numbers</p> <pre><code>shuffleArray(a); // a = e.g. [4, 8, 2, 5, ...] for (i = 0; i &lt; nodes1.length; ++i) { elements1.appendChild(nodes1[a[i]]); // append in new order elements2.appendChild(nodes2[a[i]]); } </code></pre> <p><a href="http://jsfiddle.net/H4Crr/3/" rel="nofollow"><strong>Demo</strong></a>. I've ignored <code>var</code> but remember to use it as applicable.</p> <hr> <p>Decided to add a modified version of the shuffle for 2 <em>Arrays</em>, as this will most likely be much faster.</p> <pre><code>function shuffleTwoArrays(a, b) { // Shuffle 2 arrays same length in same way var i = a.length, t, j; while (--i) { // loop over each item in array (except 0 because no point) j = (Math.random() * (i+1)) | 0; // random Int j &lt;= i t = a[i], a[i] = a[j], a[j] = t; // swap items i and j in a t = b[i], b[i] = b[j], b[j] = t; // swap items i and j in b } } </code></pre> <p>Here you would just do </p> <pre><code>shuffleTwoArrays(nodes1, nodes2); // shuffle both the same for (i = 0; i &lt; nodes1.length; ++i) { elements1.appendChild(nodes1[i]); // re-append to achieve new order elements2.appendChild(nodes2[i]); } </code></pre> <p><a href="http://jsfiddle.net/H4Crr/4/" rel="nofollow"><strong>Demo</strong></a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload