Last active
February 5, 2017 22:46
-
-
Save sean-codes/cc81441621234001d4bd2b449f03521b to your computer and use it in GitHub Desktop.
Button select random from array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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