Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forms

Status
Not open for further replies.

Berns

IS-IT--Management
Apr 25, 2002
41
US
I need help developing a script that will rotate the order of the selections on a form. For example, if the question is How old are you and the selections (radio buttons) are 12, 13, and 14. They may appear in that order or in any other order such as 13, 12, 14 etc.

I have no clue where to begin, can anyone get me started?
Thanks!
 
Is there any pattern to the order or is it random?

Kev
[Afro2]
 
Completely random. I'm not sure if this is even possible...
 
OK this may look a bit weird (mainly because you need the random number functions) but this should work

<SCRIPT>
var arrayOfOptions = [&quot;10&quot;,&quot;11&quot;,&quot;13&quot;,&quot;14&quot;, &quot;anything&quot;];

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.floor(rnd()*number);
};

function writeRadioButton() {
while (arrayOfOptions.length > 0) {
var randOption = rand(arrayOfOptions.length);
document.write('<INPUT TYPE=&quot;RADIO&quot; NAME=&quot;MYOPTIONS&quot; > ' + arrayOfOptions[randOption ] + '<BR>');
arrayOfOptions[randOption] = arrayOfOptions[arrayOfOptions.length - 1]
--arrayOfOptions.length;
}
}
</SCRIPT>
<BODY>
Please select an answer: <BR><BR>
<SCRIPT>
writeRadioButton()
</SCRIPT>
</BODY>

Just change the values in &quot;arrayOfOptions&quot; to have different buttons (not limit on number).

Hope that helps,

Kev
[Afro2]
 
Thanks, looks like this may work. How would I implement it to more then one question? Would I create a new list of arrays?

I really appreciate your help!
 
Just to clarify, I got it working properly on the first question, but there are three questions, each with their own selection of answers. If I try to apply this to questions 2 & 3 I get an error.
Thanks for your help!
 
Nice and easy change or add the following lines :

1-Add a new array
var NewOptions = [&quot;OTHER&quot;,&quot;OPTIONS&quot;];

2-Change the writeRadioButton function to :

function writeRadioButton(name, array) {
while (array.length > 0) {
var randOption = rand(array.length);
document.write('<INPUT TYPE=&quot;RADIO&quot; NAME=&quot;' + name + '&quot; > ' + array[randOption ] + '<BR>');
array[randOption] = array[arrayOfOptions.length - 1]
--array.length;
}
}

3-Then in then html change to :
<SCRIPT>
writeRadioButton(&quot;MYOPTIONS&quot;, arrayOfOptions)
</SCRIPT>

4-and add this where you want it :
<SCRIPT>
writeRadioButton(&quot;MYOTHEROPTIONS&quot;, NewOptions)
</SCRIPT>

You can then add others by making a new array and adding the above into your html changing the string and the array you pass (i.e. stages 1 and 4)

Hope that make sence

Kev
[Afro2]
 
I'm sorry, but I'm not getting it, I keep generating an error. this is what I have:
<SCRIPT>
var arrayOfOptions = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;];
var NewOptions = [&quot;OTHER&quot;,&quot;4&quot;,&quot;5&quot;, &quot;6&quot;,&quot;7&quot;];

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.floor(rnd()*number);
};

function writeRadioButton(name, array) {
while (array.length > 0) {
var randOption = rand(array.length);
document.write('<INPUT TYPE=&quot;RADIO&quot; NAME=&quot;' + name + '&quot; > ' + array[randOption ] + '<BR>');
array[randOption] = array[arrayOfOptions.length - 1]
--array.length;
}
}

</SCRIPT>
</head>
<BODY>
<form>
<p>Question 1<br>
<SCRIPT>
writeRadioButton(&quot;MYOPTIONS&quot;, arrayOfOptions)
</SCRIPT>


<p>Question 2<br>
<SCRIPT>
writeRadioButton(&quot;MYOTHEROPTIONS&quot;, NewOptions)
</SCRIPT>

Again, I appreciate your help.
 
Duh- Sorry - I got it:
<SCRIPT>
var arrayOfOptions = [&quot;1&quot;,&quot;2&quot;,&quot;3&quot;];
var NewOptions = [&quot;4&quot;,&quot;5&quot;, &quot;6&quot;,&quot;7&quot;];

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
};

function rand(number) {
return Math.floor(rnd()*number);
};

function writeRadioButton(name, array) {
while (array.length > 0) {
var randOption = rand(array.length);
document.write('<INPUT TYPE=&quot;RADIO&quot; NAME=&quot;' + name + '&quot; > ' + array[randOption ] + '<BR>');
array[randOption] = array[arrayOfOptions.length - 1]
--array.length;
}
}

</SCRIPT>
</head>
<BODY>
<form>
<p>Question 1<br>
<SCRIPT>
writeRadioButton(&quot;MYOPTIONS&quot;, arrayOfOptions)
</SCRIPT>


<p>Question 2<br>
<SCRIPT>
writeRadioButton(&quot;MYOTHEROPTIONS&quot;, NewOptions)
</SCRIPT>

Don't know where my head was, thanks!
 
Okay, now if I put in the actual answers & question I am getting an error stating that the 'ArrayOfOptions' (and the other arrays) are undefined. Even though the script that I pasted above works perfectly. Also, when I refresh the page several times some of the values for the radio buttons say &quot;undefined&quot;.

Any help is appreciated.
Thanks!
 
Sorry my bug.

In the WriteDarioButtons function you need to change the array[randOption].... line to :

array[randOption] = array[array.length - 1]

Kev
[afro2]
 
Hi that worked perfectly.

Is there a way that I can add a value to each answer. I tried adding it in the array and in this part:

function writeRadioButton(name, array) {
while (array.length > 0) {
var randOption = rand(array.length);
document.write('<INPUT TYPE=&quot;RADIO&quot; NAME=&quot;' + name + '&quot; > ' +
array[randOption] + '<BR>');
array[randOption] = array[array.length - 1]
--array.length;


Neither worked. Not sure what I can do. I need each radio button to pass the value.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top