JavaScript: Randomize Node Children

By Xah Lee. Date: . Last updated: .

Here is a fast function to randomize a element's children. The element can be ul or ol list or any element.


Here is the code.

The code is fast, tested on over 1 thousand children elements.

const xah_randomize_children_f = ((nodeX) => {
    // nodeX can be any html element
    // randomize its children
    // version 2017-05-11

    const newNode = nodeX.cloneNode(true);
    const xChildren = newNode.children;
    const newNodeFrag = document.createDocumentFragment();

    while (xChildren.length > 0) {
        newNodeFrag.appendChild( xChildren [Math.floor(Math.random() * xChildren.length)] );

    nodeX.innerHTML = "";

[see Replace All Children, createDocumentFragment]

JavaScript: Random

JavaScript in Depth

DOM Scripting


DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc