Z
zapzapzapzap64
Ok, so here's the problem. I have a series of forms on a page,
dynamically generated by php. They represent a multiple choice
question and an empty div for an answer.
They are generated to appear like this:
<form class="standard" action="" method="post" name="question1"
id="question1">
<fieldset>
<input name="q1" id="q1_ans1" value="q1_ans1" type="radio">
<label for="">Possible 1 Praesent luctus aliquet turpis. et
magnis dis parturient montes, nascetur ridiculus mus.</label>
<input class="radio_floated" name="q1" id="q1_ans2"
value="q1_ans2" type="radio">
<label for="">Possible 2 lorem penatibus et magnis dis
parturient montes</label>
<input name="q1" id="q1_ans3" value="q1_ans3" type="radio">
<label for="">Possible 3 lorem penatibus In egestas lacinia
metus. Etiam imperdiet turpis non justo.</label>
</fieldset>
<input name="q1answer" id="q1answer" value="q1_ans3"
type="hidden">
<div id="markanswer_1" class="markanswer"><a
href="javascript:markAnswer(1)"><span>mark answer</span></a></div>
</form>
The next set of questions has identical markup except question 1
becomes question2, q1 becomes q2 etc
The problem I have is that the function markAnswer needs to determine
the correct answer and the supplied answer.
Correct answer is easy, as it is stored in the hidden field in the
form:
function markAnswer(q) {
//get value of correct answer
correct = document.getElementById('q' + q + 'answer');
correctanswer = correct.value;
But I'm having trouble getting the selected radio button.
If I knew that the form being analysed was say, the first one, then I
would simply put:
for( i = 0; i < document.question1.q1.length; i++ ) {
if( document.question1.q1.checked == true )
val = document.question1.q1.value;
}
if (correctanswer == val) {
newText = document.createTextNode('correct') ;
}
This determines if the value of answer in the hidden field matches the
value of the selected radio button.
But I can't seem to manage specifying which form/radio set to analyse,
using the variable 'q' supplied by the markAnswer function.
I have tried:
//get value of selected radio button
var whichform;
whichform = ('question' + q);
var whichelement;
whichelement = ('q' + q);
for( i = 0; i < document.whichform.whichelement.length; i++ ) {
if( document.whichform.whichelement.checked == true )
val = document.whichform.whichelement.value;
}
but no luck. I presume it something to do with whichform and
whichelement being variables and being treated as such in the
document.whichform bit.
Does anyone have any ideas?
dynamically generated by php. They represent a multiple choice
question and an empty div for an answer.
They are generated to appear like this:
<form class="standard" action="" method="post" name="question1"
id="question1">
<fieldset>
<input name="q1" id="q1_ans1" value="q1_ans1" type="radio">
<label for="">Possible 1 Praesent luctus aliquet turpis. et
magnis dis parturient montes, nascetur ridiculus mus.</label>
<input class="radio_floated" name="q1" id="q1_ans2"
value="q1_ans2" type="radio">
<label for="">Possible 2 lorem penatibus et magnis dis
parturient montes</label>
<input name="q1" id="q1_ans3" value="q1_ans3" type="radio">
<label for="">Possible 3 lorem penatibus In egestas lacinia
metus. Etiam imperdiet turpis non justo.</label>
</fieldset>
<input name="q1answer" id="q1answer" value="q1_ans3"
type="hidden">
<div id="markanswer_1" class="markanswer"><a
href="javascript:markAnswer(1)"><span>mark answer</span></a></div>
</form>
The next set of questions has identical markup except question 1
becomes question2, q1 becomes q2 etc
The problem I have is that the function markAnswer needs to determine
the correct answer and the supplied answer.
Correct answer is easy, as it is stored in the hidden field in the
form:
function markAnswer(q) {
//get value of correct answer
correct = document.getElementById('q' + q + 'answer');
correctanswer = correct.value;
But I'm having trouble getting the selected radio button.
If I knew that the form being analysed was say, the first one, then I
would simply put:
for( i = 0; i < document.question1.q1.length; i++ ) {
if( document.question1.q1.checked == true )
val = document.question1.q1.value;
}
if (correctanswer == val) {
newText = document.createTextNode('correct') ;
}
This determines if the value of answer in the hidden field matches the
value of the selected radio button.
But I can't seem to manage specifying which form/radio set to analyse,
using the variable 'q' supplied by the markAnswer function.
I have tried:
//get value of selected radio button
var whichform;
whichform = ('question' + q);
var whichelement;
whichelement = ('q' + q);
for( i = 0; i < document.whichform.whichelement.length; i++ ) {
if( document.whichform.whichelement.checked == true )
val = document.whichform.whichelement.value;
}
but no luck. I presume it something to do with whichform and
whichelement being variables and being treated as such in the
document.whichform bit.
Does anyone have any ideas?