JS: Randomize Node Children π
By Xah Lee. Date: . Last updated: .
- Here is a function to randomize an element's children.
- The code is fast, tested on over 1 thousand children elements.
Example
Code
const xah_randomize_children_f = ((nodeX) => {
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);
});