Click to See Complete Forum and Search --> : Help with js and radio group


mgyavas
11-09-2003, 03:03 PM
Hi, I am trying to make a self grading test for a course project. i have prepared the js code with my little knowledge but i cant get it working. can someone look at my code?



function checkRadios() {
var q1 = document.eva.q1;
var q2 = document.eva.q2;
var q3 = document.eva.q3;
var q4 = document.eva.q4;
var q5 = document.eva.q5;
var q6 = document.eva.q6;
var total = q1+q2+q3+q4+q5+q6;
alert(total);
if( total>=25){ alert("Congratulations you learned all about matrix.");}
else if( 16<=total<=24 ){ alert("You should try to focus on some specific points of the matrix structure.".total);}
else if( 7<=total<=15){ alert("You do not understand what the matrix structure is, take a look at it once more.");}
else if(0<=total<=6){ alert("Take a look at everything once more!!!");}

}

i have 6 radiogroups and they are valued from 1 to 5.
Why does my code not working?:( :confused:

mgyavas
11-10-2003, 03:04 AM
*bump*

Gollum
11-10-2003, 04:48 AM
Radio groups are handled in Javascript by dealling with the individual radio buttons. Since all the buttons in a group have the same name, you can access them as an array (if more than on) by "document.formName.radioGroupName".

you can then use a function like this to get the current value...

function getRadioValue(grp)
{
if ( grp instanceof Array )
{
for ( var i = 0; i < grp.length; i++ )
{
if ( grp[i].checked ) return grp[i].value;
}
// default to first value
return grp[0].value;
}
else
{
return grp.value;
}
}

then you can change your function like this...

...
var q1 = getRadioValue(document.eva.q1);
...

mgyavas
11-10-2003, 08:42 AM
thank you Gollum, i will try:)