You can do it this way as well:
Here's a fiddle to try it out: http://jsfiddle.net/bhuVH/
HTML Code:
<!DOCTYPE html>
<html>
<body>
<button id="randomTrickButton">Randomize Tricks</button>
<div id="blackboard"></div>
<script>
// From http://stackoverflow.com/questions/962802/is-it-correct-to-use-javascript-array-sort-method-for-shuffling/962890#962890
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
var trickArray = [],
i,
bBoard = document.getElementById('blackboard'),
trickBtn = document.getElementById('randomTrickButton');
trickArray.push({id:'stance', txt:'Regular'});
trickArray.push({id:'flip',txt: '360 shov-it'});
trickArray.push({id:'direction',txt:'Backside'});
// EventListener for non-IE browsers
trickBtn.addEventListener(
'click',
function(e) {
bBoard.innerHTML = '';
trickArray = shuffle(trickArray);
for( i = 0; i < trickArray.length; i++ ) {
var t = trickArray[i];
bBoard.innerHTML += '<div id="' + t.id + '">' + t.txt + '</div>';
}
},
false
);
</script>
</body>
</html>
Bookmarks