SO Answer / Clone a div and make ID's of it it's children to be unique using javascript

See: http://stackoverflow.com/questions/10832624/clone-a-div-and-make-ids-of-it-its-children-to-be-unique-using-javascript

by KooiInc MeHere

HTML

<div id="someDiv">
<p id="sdp1"></p>
<p id="sdp2"><span>innerspan</span></p>
<input type="text" value="testmyvalue">
</div>


<div id="result"><b>HTML of the clone now shows</b>:<br></div>

CSS

body {margin:1em;font:0.8em normal verdana,arial}

JavaScript

var someClone = someDiv.cloneNode(true), children = someClone.childNodes;
someClone.id = '_'+Math.floor(1000+Math.random()*10000).toString(16);
reId(children);

function reId(nodes){
 for (var i=0;i<nodes.length;(i+=1)){
   var children = nodes[i].childNodes;
    if(nodes[i].nodeType !== 3) {
     nodes[i].id = '_'+(Math.floor( 1001+Math.random()*10000 )).toString(16);
    }
   if (children.length){
       reId(children);
   }
 }
}  
document.body.insertBefore(someClone,document.body.childNodes[1]);
document.querySelector('#result').innerHTML += someClone.outerHTML.replace(/</g,'&lt;');