-1

I'm writing a form for a personality test. The page has questions where the user would need to choose an answer for each. The page will not allow the user to skip questions and it will alert them to answer the missing questions. When the form is submitted, the page will show the score.

I'm trying to store the values of radio buttons in an array using a for loop. It is not working.

Here is the HTML code:

<!-- Form -->
<form name="quiz" onsubmit="return show()">

    <!-- Question 1 -->
    <p>Question 1</p>
    <input type="radio" name="q0" value="a"> a
    <br>
    <input type="radio" name="q0" value="b"> b
    <br>
    <input type="radio" name="q0" value="c"> c

    <!-- Question 2 -->

    <!-- Question 3 -->

    <!-- Question 4 -->

    <!-- Question 5 -->
    <p>Question 5</p>
    <input type="radio" name="q4" value="a"> a
    <br>
    <input type="radio" name="q4" value="b"> b
    <br>
    <input type="radio" name="q4" value="c"> c

    <!-- Submit Button -->
    <input type="submit" value="Show Result">

</form>

Here is my JavaScript code:

function show() {

    var questions = 5;
    var score = 0;
    var solution = ["a", "c", "a", "b", "b"];
    var answers = [];

    for (i = 0; i < questions; i++) {

        answers[i] = document.quiz.eval('q' + i).value;

        if (answers[i] == null || answers[i] == '') {
            window.alert("You need to answer question " + (i + 1));
            return false;
        }

        if (solution[i] == answers[i]) {
            score++;
        }

    }
    window.alert(score);
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Bilal
  • 3
  • 1
  • 3
  • what errors are you seeing in the *browser developer tools console* - I ask because `document.quiz.eval` looks like a made up function name - perhaps you wanted something like `document.forms.namedItem('quiz').elements.namedItem('q'+i).value` – Jaromanda X Nov 14 '16 at 04:47
  • Possible duplicate of [Get Radio Button Value with Javascript](http://stackoverflow.com/questions/9618504/get-radio-button-value-with-javascript) – Emile Bergeron Nov 14 '16 at 06:31

3 Answers3

0

What you need is a validating function.

// Since you mentionned
// "I'm trying to store the values of radio buttons in an array"
var answers = [];

var show = (function(answers) {
    // evaluate this once and avoid globals
    var questions = 5,
        solution = ["a", "c", "a", "b", "b"],
        form = document.quiz;

    // and return a validate function
    return function() {

        var score = 0,
            success = solution.every(function(value, i) {

                // get the value of the form field, notice the brackets
                var current = answers[i] = form['q' + i].value;
                if (current === value) {
                    score++;
                } else if (current == null || current == '') {
                    alert("You need to answer question " + (i + 1));
                    return false;
                }
                return true;
            });
        if (success) alert("Score: " + score);
        return success;
    };

})(answers);

The important part is document.quiz['q' + i]. See Get Radio Button Value with Javascript.

Community
  • 1
  • 1
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
0

eval is a property of window object and not the form , but in fact you don't actually need to use it.

Solution 1. replace

answers[i] = document.quiz.eval('q'+i).value;

with

var tmp = 'q'+i;
answers[i] = document.quiz[tmp].value;

Solution 2. replace

 answers[i] = document.quiz.eval('q'+i).value;

with

 document.quiz['q'+i].value;

NOTE: Even if you could use eval like the way you wanted (using prototypes) it would still give error because on this line

 answers[i] = document.quiz.eval('q'+i).value;

suppose i=0 eval would try to evaluate q0 (treating it as a named variable) and you don't have any variable with that name in your global scope

Vinay
  • 7,442
  • 6
  • 25
  • 48
0

You need to loop through each of the radio button options for each radio group to find the checked input and get it's value. At the moment you are just looping through each group.

Add this at the beginning of your for loop:

var radioButtons = document.getElementsByName("q"+i);
for (var x = 0; x < radioButtons.length; x ++) {
  if (radioButtons[x].checked) {
   answers[i] = radioButtons[x].value;
 }
}
partypete25
  • 4,353
  • 1
  • 17
  • 16