Can't Validate Retun for Radio Button

D

David Jubinville

Hi All,

I'm developing a 'testing' page for the current courseware solution I'm
working on. Currently I have a single page with each question residing in
it's own layer <DIV>. I want to make sure the users have selected the
correct answer before the next DIV appears. The validation code works fine
and there are no problems there. For what ever reason, I can't seem to get
it to return valid data as its value. Something like this:


<input type="button" name="Submit" value="Submit" class="btn" onClick="
if (document.q1.chq1[0].checked) {answer = 0};
if (document.q1.chq1[1].checked) {answer = 1};
if (document.q1.chq1[2].checked) {answer = 2};
if (document.q1.chq1[3].checked) {answer = 3};
if (document.q1.chq1[4].checked) {answer = 4};
alert ('Your answer was ' + document.q1.chq1[answer].value);
if (document.q1.chq1[answer].value='correct') {document.q1.style.visibility
= 'hidden'; document.q2.style.visibility = 'visible'}" />

And so on and so forth, until the end of the questions... At this point it
works fine - however - it progresses to the next question regardless of the
value (correct / incorrect).

Is there something I'm missing here?

I know there are probably a million better methods for testing this data, if
you feel so inclined as to let me know, I certainly would be appreciative.

<Big nasty source snippit below>

Thanks in advance!
David

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="q1">
<table width="65%" border="0" align="center" cellpadding="1"
cellspacing="0" style="font-size:85%; font-family:Geneva, Arial, Helvetica,
sans-serif;">
<tr>
<td height="50" align="center"
valign="middle" style=" border-bottom-width:1px; border-bottom-style:solid;
border-bottom-color:#FFCD00; border-right-width:1px;
border-right-style:solid; border-right-color:#FFCD00;
background-color:#F3F7F9;"><span class="bodyheadLarge">Q1</span></td>
<td height="50" colspan="3"
valign="bottom" style=" border-bottom-width:1px; border-bottom-style:solid;
border-bottom-color:#FFCD00; padding-left:0.1in" class="bodycopyRed">What
is the carrier signal used by
Digital
Cable Called?</td>
</tr>
<tr align="center" valign="top">
<td width="10%" rowspan="6"
style="border-right-width:1px; border-right-style:solid;
border-right-color:#FFCD00; background-color:#F3F7F9;;">&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2"
align="left">&nbsp;</td>
</tr>
<tr align="center" valign="top">
<td width="8%"> <input type="radio"
name="chq1" value="incorrect" /></td>
<td width="82%" colspan="2"
align="left">Phase
Shift</td>
</tr>
<tr align="center" valign="top">
<td width="8%"> <input type="radio"
name="chq1" value="incorrect" /></td>
<td colspan="2" align="left">Digital
Signal</td>
</tr>
<tr align="center" valign="top">
<td width="8%"> <input type="radio"
name="chq1" value="correct" /></td>
<td colspan="2"
align="left">Quadrature
Amplitude Modulation (QAM)</td>
</tr>
<tr align="center" valign="top">
<td width="8%"> <input type="radio"
name="chq1" value="incorrect" /></td>
<td colspan="2" align="left">Analogue
Signal</td>
</tr>
<tr align="center" valign="top">
<td width="8%"> <input type="radio"
name="chq1" value="incorrect" /></td>
<td colspan="2" align="left">Amplitude
Modulation</td>
</tr>
<tr align="center" valign="top">
<td style="border-right-width:1px;
border-right-style:solid; border-right-color:#FFCD00;
background-color:#F3F7F9;;">&nbsp;</td>
<td>&nbsp;</td>
<td colspan="2"
align="left">&nbsp;</td>
</tr>
<tr align="center" valign="top">
<td colspan="4"
style="background-color:#F3F7F9;"><div align="right">
<input type="button" name="Submit"
value="Submit" class="btn" onClick="if (document.q1.chq1[0].checked) {answer
= 0}; if (document.q1.chq1[1].checked) {answer = 1}; if
(document.q1.chq1[2].checked) {answer = 2}; if (document.q1.chq1[3].checked)
{answer = 3}; if (document.q1.chq1[4].checked) {answer = 4}; alert ('Your
answer was ' + document.q1.chq1[answer].value); if
(document.q1.chq1[answer].value='correct') {document.q1.style.visibility =
'hidden'; document.q2.style.visibility = 'visible'}">
</div></td>
</tr>
</table>
</form>
</body>
</html>
 
D

devniall

David said:
<input type="button" name="Submit" value="Submit" class="btn" onClick="
if (document.q1.chq1[0].checked) {answer = 0};
if (document.q1.chq1[1].checked) {answer = 1};
if (document.q1.chq1[2].checked) {answer = 2};
if (document.q1.chq1[3].checked) {answer = 3};
if (document.q1.chq1[4].checked) {answer = 4};
alert ('Your answer was ' + document.q1.chq1[answer].value);
if (document.q1.chq1[answer].value='correct') {document.q1.style.visibility
= 'hidden'; document.q2.style.visibility = 'visible'}" />

And so on and so forth, until the end of the questions... At this point it
works fine - however - it progresses to the next question regardless of the
value (correct / incorrect).

Hi David,

"if (document.q1.chq1[answer].value='correct')" will always evaluate as
"true" since "=" is an assignment operator, not a comparison operator.
You probably meant to type "if
(document.q1.chq1[answer].value=='correct')".
 
D

David Jubinville

David said:
<input type="button" name="Submit" value="Submit" class="btn" onClick="
if (document.q1.chq1[0].checked) {answer = 0};
if (document.q1.chq1[1].checked) {answer = 1};
if (document.q1.chq1[2].checked) {answer = 2};
if (document.q1.chq1[3].checked) {answer = 3};
if (document.q1.chq1[4].checked) {answer = 4};
alert ('Your answer was ' + document.q1.chq1[answer].value);
if (document.q1.chq1[answer].value='correct') {document.q1.style.visibility
= 'hidden'; document.q2.style.visibility = 'visible'}" />

And so on and so forth, until the end of the questions... At this point it
works fine - however - it progresses to the next question regardless of the
value (correct / incorrect).

Hi David,

"if (document.q1.chq1[answer].value='correct')" will always evaluate as
"true" since "=" is an assignment operator, not a comparison operator.
You probably meant to type "if
(document.q1.chq1[answer].value=='correct')".
Hi Niall,

Good call. Thanks for pointing that out! It kinda goes a bit against logic
to verify the state of a value while setting it... oops.

In other news, unfortunatly this did not help solve the problem, but made it
obvious that it was a two fold issue here.

Thanks again,
David
 
L

Lee

David Jubinville said:
Hi All,

I'm developing a 'testing' page for the current courseware solution I'm
working on.

Before you put much more work into this, you might want to consider whether or
not it's a problem than web-savvy users will simply look at the source of the
page to see which answers are correct.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top