JS: Randomize Node Children πŸ’ 

By Xah Lee. Date: . Last updated: .

Example

Code

const xah_randomize_children_f = ((nodeX) => {
    // nodeX can be any html element
    // randomize its children
    // http://xahlee.info/js/js_dom_randomize_list.html
    // 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 = "";
    nodeX.appendChild(newNodeFrag);
});

JavaScript, Random