-
Hi all,
does anyone know how to scramble the "box#" in the codes below? Ultimately, I want the checkboxes to be in different order each time I access the web page?
Thanks, Kenny
<script lanugage="javascript">
function countChoices(obj) {
max = 3; // max. number allowed at a time
box1 = obj.form.box1.checked; // your checkboxes here
box2 = obj.form.box2.checked;
box3 = obj.form.box3.checked; // add more if necessary
box4 = obj.form.box4.checked;
box5 = obj.form.box5.checked;
count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0) + (box4 ? 1 : 0) + (box5 ? 1 : 0);
if (count > max) {
alert("You can only choose " + max + " of the 5 nominees.");
obj.checked = false;
}
}
</script>
<form>
<input type=checkbox name=box1 onClick="countChoices(this)"> Homer Simpsons <p>
<input type=checkbox name=box2 onClick="countChoices(this)"> Ground's Keeper Willie <p>
<input type=checkbox name=box3 onClick="countChoices(this)"> Mr. Burns <p>
<input type=checkbox name=box4 onClick="countChoices(this)"> Barney <p>
<input type=checkbox name=box5 onClick="countChoices(this)"> Chief Wiggum<p>
</form>
-
Hi Kenny,
I'm a little slow tonight, so....
Do you mean you want a different person listed for the first checkbox?
Regarding title of your post: the following should put a unique number within each array cell. Note that it continues to generate random numbers until the array is filled. And if the random number is already in the array, it simply generates a new one. (I fiddled this from one of the script/tutorials at my site -- "AD&D Character Generation" -- so it may have a bug or two (god forbid!)
function rollIt(maxNum, myArray)
{
var randNum = num = 0;
var numAdjustment = 1;
var notDone = true;
do
{
randNum += Math.floor( Math.random() * maxNum) + numAdjustment;
for (i = 0; i < myArray.length; i++)
{
if (randNum == myArray[i]) break;
else
{
myArray[i] = randNum;
num++;
}
if (num == myArray.length) notDone = false;
}
} while(notDone);
}
Vinny