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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button Array

Status
Not open for further replies.

BLewisSSA

Programmer
Mar 13, 2002
23
US
I have hit a problem that I was told requires a Radio Button Array but I have never done one on the web and I am unsure what it is supposed to look like. I am writing a training site for the intranet and after each section I need to do a couple of multiple choice review questions. My thought was to have a radio button next to each response and if they select the right one then it navigates to a page that tells them they are right and why that is the right answer and if they choose the wrong one then have it navigate to a page that tells them they are wrong and why and what the right answer is. If anyone can help me with this or give me a better suggestion on how to make it work that would be great. Brad Lewis

It is in moments of decision that our destiny is forged.
 
Maybe something like this:

<script>
function openit() {

if (form1.choice[0].checked) {
window.location.href=&quot;rightanswer.html&quot;;
}
else {
window.location.href=&quot;wronganswer.html:.
}
}
</script>

The '0' in choice[0], referes to the first radio button if that is the right answer...if the right answer is the second radio button, then it would be: choice[1]...if third then, choice[2]...
'Choice' is what I called my radio button... I have not failed; I merely found 100,000 different ways of not succeding...
 
Thanks that does help. I have a question though. You named the the radio buttons choice did you name the first one choice[0] or is there a way to name all of them together choice and then the number is assigned based on position? I know in Visual Basic you can name them all one thing but I am not sure about this. If you could elaborate I would appreciate it. Brad Lewis

It is in moments of decision that our destiny is forged.
 
Never mind I was thinking to much. I figured that part out. I think I have it now. Brad Lewis

It is in moments of decision that our destiny is forged.
 
It's just like you learned for VB, with all of the related radio buttons being named the same thing and the array index depends on the position and is automatically assigned.
 
This is a quick version of what I am doing. I had to change info but you get the point. I don't know what I am doing wrong but it isn't working. Any ideas?

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<form method=post name=&quot;form1&quot; id=&quot;form1&quot;>
<script>
function openit() {

if (form1.choice[4].checked) {
window.location.href=&quot;rightanswer.asp&quot;;
}
else {
window.location.href=&quot;wronganswer.asp:.
}
}

</script>


<P>What is the best kind of Ice Cream?</P>
<INPUT type=&quot;radio&quot; id=choice name=choice><OPTION>Rocky Road</OPTION><BR>
<INPUT type=&quot;radio&quot; id=choice name=choice><OPTION>Moose Tracks</OPTION><BR>
<INPUT type=&quot;radio&quot; id=choice name=choice><OPTION>Chocolate</OPTION><BR>
<INPUT type=&quot;radio&quot; id=choice name=choice><OPTION>Vanilla</OPTION><BR>
<INPUT type=&quot;radio&quot; id=choice name=choice><OPTION>Chunky Monkey</OPTION>
</form>
</BODY>
</HTML>
Brad Lewis

It is in moments of decision that our destiny is forged.
 
Here ya go:

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>

<form method=post name=&quot;form1&quot; id=&quot;form1&quot; onClick=&quot;return openit();&quot;>
<script>
function openit() {

if (document.form1.choice[4].checked) {
window.location.href=&quot;rightanswer.asp&quot;;
}
else {
window.location.href=&quot;wronganswer.asp&quot;;
}
}

</script>


<P>What is the best kind of Ice Cream?</P>
<INPUT type=&quot;radio&quot; id=choice name=choice>
Rocky Road<BR>
<INPUT type=&quot;radio&quot; id=choice name=choice>
Moose Tracks<BR>
<INPUT type=&quot;radio&quot; id=choice name=choice>
Chocolate<BR>
<INPUT type=&quot;radio&quot; id=choice name=choice>
Vanilla<BR>
<INPUT type=&quot;radio&quot; id=choice name=choice>
Chunky Monkey
</form>
</BODY>
</HTML>

You made a couple mistakes...
mistake 1) You had <option> tags in with the input type...
mistake 2) You forgot to call the function in the <form> tag...
mistake 3) You forgot to add the closing &quot; in the else clause...
Hope it helps... I have not failed; I merely found 100,000 different ways of not succeding...
 
Thank you very much. That worked perfectly. Brad Lewis

It is in moments of decision that our destiny is forged.
 
Glad I could help ya... I have not failed; I merely found 100,000 different ways of not succeding...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top