I have created an string array, that hold 6 classes.
The function is called by a button, and for every click a new class i created.
the thing is that when i click the button, i get a randomize class, but if i press the button again. the same class is being displayed and not a random one. If i reload the page, an random class is generated, but as i said, the same class generates when i click again.
This is my random array.
var Array=["dice dice-side-one",
"dice dice-side-two",
"dice dice-side-three",
"dice dice-side-four",
"dice dice-side-five",
"dice dice-side-six",
"dice dice-side-six"];
var rand = Array[Math.floor(Math.random() * Array.length)];
function addDiceEvent() {
dicesides_func(rand);
}
}
I paste the full function if the problem is there somewhere.
function AddEvent(){
var AddEvent = "add";
var addClassArr= document.getElementsByClassName(AddEvent);
for(var i=0; i<addClassArr.length; i++){
var addClass = addClassArr[i];
addClass.addEventListener("click", addDiceEvent, true);
}
var Array=["dice dice-side-one",
"dice dice-side-two",
"dice dice-side-three",
"dice dice-side-four",
"dice dice-side-five",
"dice dice-side-six",
"dice dice-side-six"];
var rand = Array[Math.floor(Math.random() * Array.length)];
function addDiceEvent() {
dicesides_func(rand);
}
}
AddEvent();
Just do the random thing every time the function is called, not outside of it: