I have multiple ul elements on my page and I need to add a class name to some and those with that class name need to randomize. I can do it by id but I can only have one id on a page so I can't do that. I need a JavaScript solution without JQuery. Any help is appreciated. Thank you.
Here is what I used to randomize it by id (it only does one ul though):
var thisUL = document.getElementById("myID");
for (var i = thisUL .children.length; i >= 0; i--) {
thisUL.appendChild(thisUL.children[Math.random() * i | 0]);
}
<ul id="myID">
<li>random1a</li>
<li>random1b</li>
<li>random1c</li>
</ul>
<ul id="myID">
<li>random2a</li>
<li>random3b</li>
<li>random4c</li>
</ul>
<ul id="myID">
<li>random3a</li>
<li>random3b</li>
<li>random3c</li>
</ul>
Each li would need to stay within its own ul.
Aucun commentaire:
Enregistrer un commentaire