0

I have an HTML list like this:

<ul>
<li>This is an item.</li>
<li>This is another item.</li>
<li>This is yet another item.</li>
</ul>

How can I have these items displayed in a random order each time the document is loaded?

Village
  • 20,305
  • 41
  • 116
  • 158

2 Answers2

1

Add this javascript:

var ul = document.getElementById("list");
for (var i = ul.children.length; i >= 0; i--)
ul.appendChild(ul.children[Math.random() * i | 0]);

DEMO

(This answer was originally provided by Alexey Lebedev for this question: javascript - shuffle HTML list element order)

Community
  • 1
  • 1
Tarun
  • 581
  • 5
  • 17
1

As it has answer here javascript - shuffle HTML list element order

var ul = document.getElementById("item");

for (var i = ul.children.length; i >= 0; i--) { ul.appendChild(ul.children[Math.random() * i | 0]); }

Community
  • 1
  • 1
Neel
  • 11,427
  • 3
  • 42
  • 60