0

In my spelling game I have a list of words that are pulled from the HTML. I can choose how many I want to pull out to populate the grid each time the program is run, but the problem is if I choose to take 6 words from the list it pulls out the first 6, not any random 6. Can someone tell me why this is happening?

Here is the that holds the words...

<ul style="display:none;" id="wordlist">

    <li data-word="mum" data-audio="http://www.wav-sounds.com/cartoon/porkypig2.wav" data-pic="http://www.clker.com/cliparts/5/e/7/f/1195445022768793934Gerald_G_Lady_Face_Cartoon_1.svg.med.png"></li>

    <li data-word="cat" data-audio="http://www.wav-sounds.com/cartoon/bugsbunny2.wav" data-pic="http://www.clker.com/cliparts/c/9/9/5/119543969236915703Gerald_G_Cartoon_Cat_Face.svg.med.png"></li>

    <li data-word="dog" data-audio="http://www.wav-sounds.com/cartoon/daffyduck1.wav" data-pic="http://www.clker.com/cliparts/a/2/c/0/1195440948271207911zeimusu_spotty_dog.svg.med.png"></li>

    <li data-word="bug" data-audio="http://www.wav-sounds.com/cartoon/daffyduck2.wav" data-pic="http://www.clker.com/cliparts/4/b/4/2/1216180545881311858laurent_scarabe.svg.med.png"></li>

    <li data-word="rat" data-audio="http://www.wav-sounds.com/cartoon/bugsbunny1.wav" data-pic="http://www.clker.com/cliparts/C/j/X/e/k/D/mouse-md.png"></li>

    <li data-word="dad" data-audio="http://www.wav-sounds.com/cartoon/porkypig1.wav" data-pic="http://www.clker.com/cliparts/3/a/6/6/119544474191128182Gerald_G_Man_Face_6_-_World_Label.svg.med.png"></li>

    <li data-word="mouse" data-audio="http://www.wav-sounds.com/cartoon/bugsbunny1.wav" data-pic="http://www.clker.com/cliparts/C/j/X/e/k/D/mouse-md.png"></li>

    <li data-word="dad" data-audio="http://www.wav-sounds.com/cartoon/porkypig1.wav" data-pic="http://www.clker.com/cliparts/3/a/6/6/119544474191128182Gerald_G_Man_Face_6_-_World_Label.svg.med.png"></li>

</ul>

The script is rather long so here is a fiddle http://jsfiddle.net/smilburn/ZAfVZ/

m0onio
  • 213
  • 1
  • 14
  • 1
    To pick-up some elements randomly, have a look [here](http://stackoverflow.com/questions/1764160/jquery-select-random-elements) – Stphane Sep 18 '12 at 11:50

1 Answers1

2

Modified code for you from this answer

randomElements = jQuery("#wordlist li").get().sort(function(){ 
  return Math.round(Math.random())-0.5
}).slice(0,6)
Community
  • 1
  • 1
Anoop
  • 23,044
  • 10
  • 62
  • 76