PDA

View Full Version : Cant get radio buttons code to work



charco
10-09-2006, 06:57 PM
Hi all,

This is driving me crazy. I have a code that generates radio buttons in a form to then se them in a quiz. I have randomised the position of the buttons and can't get the function that reads the value from the radio buttons to match my own 'answer' - anybody please?

The code that generates the form elements:
def[j][x] comes from an array
------------------------------------------
ans=def[j][0]
if (j<13){
choiceB=def[j+2][0]
choiceC=def[j+1][0]
choiceD=def[j+3][0]
}
else{
choiceB=def[j-2][0]
choiceC=def[j-1][0]
choiceD=def[j-3][0]
}

scram=Math.round(3*Math.random())
if (scram==0){
var choice1=ans
var choice2=choiceB
var choice3=choiceC
var choice4=choiceD
}
else if(scram==1){
var choice1=choiceB
var choice2=ans
var choice3=choiceC
var choice4=choiceD
}
else if(scram==2){
var choice1=choiceB
var choice2=choiceC
var choice3=ans
var choice4=choiceD
}
else if(scram==3){
var choice1=choiceB
var choice2=choiceC
var choice3=choiceD
var choice4=ans
}
------------------------------------------------------------
//now I generate the form elements and set them into the form

------------------------------------------------------------
var choice1HTML=''
choice1HTML+=choice1
document.getElementById('choice1').innerHTML=choice1HTML
var radio1HTML=''
radio1HTML+='<input type="radio" name=mychoice onClick="GetSelectedItem()" value=choice1>'
document.getElementById('radio1').innerHTML=radio1HTML
var choice2HTML=''
choice2HTML+=choice2
document.getElementById('choice2').innerHTML=choice2HTML
var radio2HTML=''
radio2HTML+='<input type="radio" name=mychoice onClick="GetSelectedItem()" value=choice2>'
document.getElementById('radio2').innerHTML=radio2HTML
var choice3HTML=''
choice3HTML+=choice3
document.getElementById('choice3').innerHTML=choice3HTML
var radio3HTML=''
radio3HTML+='<input type="radio" name=mychoice onClick="GetSelectedItem()" value=choice3>'
document.getElementById('radio3').innerHTML=radio3HTML
var choice4HTML=''
choice4HTML+=choice4
document.getElementById('choice4').innerHTML=choice4HTML
var radio4HTML=''
radio4HTML+='<input type="radio" name=mychoice onClick="GetSelectedItem()" value=choice4>'
document.getElementById('radio4').innerHTML=radio4HTML
---------------------------------------------------------------------
all of this works fine
the function GetSelectedItem() is the standard
---------------------------------------------------------------------
function GetSelectedItem(){
chosen=''
len = document.forms[0].mychoice.length
for (k = 0; k<len; k++) {
if (document.forms[0].mychoice[k].checked){
chosen=document.forms[0].mychoice[k].value}
}
}
----------------------------------------------------------------------
And the answer function is:
----------------------------------------------------------------------
function answer(){
clicks++
document.forms[0].tried.value=total
if (clicks>1){
var displayHTML=''
displayHTML+='<p class="Wans">'
displayHTML+=("Please click 'check answer' once only");
displayHTML+='</p>'
document.getElementById('hintarea').innerHTML=displayHTML
}
if (chosen==ans && clicks<2)
{
correct++
document.forms[0].result.value="correct"
document.forms[0].right.value=correct
var displayHTML=''
displayHTML+=("<img src='../img/tick.gif'>");
document.getElementById('picarea').innerHTML=displayHTML
}
else{document.forms[0].result.value="incorrect"
}
if (total==10){
document.forms[0].tried.value=total
var displayHTML=''
displayHTML+='<p class="Rans">'
displayHTML+=("Test over! You scored a total of "+correct+" out of 10 <br>");
displayHTML+='</p>'
document.getElementById('hintarea').innerHTML=displayHTML
}
}
----------------------------------------------------------------------

I figure that it's the highlighted bit that doesn't work but I dont know why not... any help please