Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Button select random from array
//Vanilla
button = document.getElementsByTagName('button')[0];
button.addEventListener('click', function(){
rotate();
});
var rotate = function(){
var sides = ['show-front', 'show-back', 'show-left', 'show-right', 'show-top', 'show-bottom'];
var side = sides[Math.floor(Math.random()*sides.length)];
for(var i in sides){
boxes = document.getElementsByClassName('box');
for(var o = 0; o < boxes.length; o++){
boxes[o].classList.remove(sides[i]);
boxes[o].classList.add(side);
}
}
}
//JQuery
$('button').on('click', function(){
rotate();
});
var rotate = function(){
//Array
var sides = ['show-front', 'show-back', 'show-left', 'show-right', 'show-top', 'show-bottom'];
//Select
var side = sides[Math.floor(Math.random()*sides.length)];
//Apply
var box = document.getElementByClassName('box');
$('.box').removeClass('show-front show-back show-left show-right show-top show-bottom').addClass(side);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment