views:

583

answers:

1

Hi,

I am trying to create a simple questionnaire for a website but am fairly new to javascript and html

I do have a basic function to calculate a percentage and have created the html form with radio buttons and a submit button

There will be about 20 questions in the questionnaire which will have yes/no radio buttons.

What I need to do is have a way of looking at each question to see if the user answered yes or no.

If they answered yes then I need to keep total to then work out how many questions they answered yes too.

After the user has pressed the submit button I then need to display a summary based on the yes answer percentage calculated above

The summary may look like the following: - To 33% and below you have ticked yes this mean THIS - To 66% and below you have ticked yes this mean SOMETHING ELSE

function CalculatePercentage() { a = document.form1.c.value; b = 10; c = a/b; d = c*100; document.form1.total2.value = d }

Could someone please point me in the right direction on how to find out if a radio button question answer is yes or not and then how to create the summary based on the percentage?

Thank you

+1  A: 

first you need to calculate all the radio button values

question1 <input type="radio" name="questions1" value="1" />Yes
 <input type="radio" name="questions" value="0" />No
question2 <input type="radio" name="questions2" value="1" />Yes
 <input type="radio" name="questions2" value="0" />No

javascript:

totalVal = 0;
//calculate the total number of yes clicked
for(y=0; y=noOfQuestion; y++)
{
    var questionNo = document.getElementsByName("questions" + y);
    for (i=0; i<questionNo.length; i++)
    {
      if (document.myform.questions[i].checked==true)
      {
        totalVal = totalVal + parseInt(document.myform.questions[i].value,10);
      }
    }
}

//calculate percentage

perc = totalVal/noOfQuestion;
perc = perc * 100;
percString = perc + "%";

to be continued

peacmaker
Hi, I've tried to implement your example but as the name on the radio buttons is the same they are mutually exclusive and remove the selection previously made.
N00b
Maybe checkboxes would work better with the code above?
N00b
try the solution now and see if its works and if not i will edit it again
peacmaker
i think your for loop is broken, no closing brace. (Ps thank u so far!)
N00b
should for (i=0; i<questionNo.length; i++) be removed?document.myform.questions[i].checked is always null i think it should be questionNo which is the radio button but then questionNo.checked is also null too
N00b
I can work with the original version using check boxes that not a problem. marked this as the answer, thanks
N00b