Reputation: 237
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
Upvotes: 1
Views: 7966
Reputation: 11
Instead of using :
document.myform.questionNo[i]
You use simply :
questionNo[i]...
I did that and I got the answer.
Upvotes: 1
Reputation: 8926
First you need to calculate all the radio button values
Question1 <input type="radio" name="questions1" value="1" />Yes<br />
<input type="radio" name="questions1" value="0" />No
Question2 <input type="radio" name="questions2" value="1" />Yes<br />
<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
Upvotes: 1