3

I am creating a multiple choice test hard coded in a form in HTML, and I'm using JavaScript and jQuery to perform all my actions related to the form. However, my onClick event doesn't seem to be firing off. Looking in the console I found that it hooks into the event with the id 'submission' and it moves to 'on' and at the click event it refreshes the browser instead of firing the submitAnswers() function.

<form name="javaScriptQuiz" id="jsTest">

 <div class="form-group quiz-questions">
 <h3>1. First question is either true or false.</h3> 
  <label for="q1a">
   <input type="radio" name="q1" value="a" id="q1a"> A. True &nbsp;
  </label>
  <label for="q1b">
   <input type="radio" name="q1" value="b"  id="q1b"> B. False &nbsp;
  </label>
  <h3>2. Second question is either true or false.</h3>
  <label for="q1a">
   <input type="radio" name="q1" value="a" id="q1a"> A. True &nbsp;
  </label>
  <label for="q1b">
   <input type="radio" name="q1" value="b"  id="q1b"> B. False &nbsp;
  </label>
 <input class="btn btn-primary btn-lg" type="submit" name="submit" value="Submit!" id="submission">

</form>

$('#submission').on('click', function() {
        console.log('Submit fires');
        submitAnswers();                    
});

function submitAnswers() {
    var total = 2;
    var score = 0;

    //captures each of the questions answers in a variable
    var q1 = document.form['javaScriptQuiz']['q1'].value;
    var q2 = document.form['javaScriptQuiz']['q2'].value;

    //Validation
    for(var i = 1; i <= total; i++ ){
        if(eval('q' + i) == null || eval('q' + i) == ''){
            alert('You missed a question');
            return false;
        }
    }

    // Are the answers to test
    var answers = ['b', 'a'];
    for(var i = 1; i <= total; i++){
        if(eval('q' + 1) == answers[i - 1] ){
            console.log('Evaluating answers');
            score ++;
        }
    }
}
1
  • 3
    change the button type from submit to button. because of submit it is submitting the form. And finally in your submit answers function if everything is right then use $('#Yourformid').submit(); Commented Dec 21, 2016 at 4:59

6 Answers 6

1

Try not to use name 'submit' for submit-type elements, this is not a good idea. But the only error I see is missing s letter in function submitAnswers(): document.form instead of document.forms. I tried you code and it works in latest Firefox.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, I took so long to answer back, but that was totally the issue. I'm still a little bit of a newbie so I appreciate the help. :)
1

You need to prevent default behavior of submit button using e.preventDefault() as follows,

$('#submission').on('click', function(e) {
        console.log('Submit fires');
        e.preventDefault();
        submitAnswers();                    
});

1 Comment

Why do you think he needs prevention? His function returns false - that's the prevention he already did. He has an error in accessing document.forms - missed s character, that's the only why he has browser undefined behavior.
1

Firs of all your HTML is not valid. The radio buttons for both the questions have same name attribute. Here's the valid markup:-

 <form name="javaScriptQuiz" id="jsTest">
 <div class="form-group quiz-questions">
 <h3>1. First question is either true or false.</h3> 
  <label for="q1a">
   <input type="radio" name="q1" value="a" id="q1a"> A. True &nbsp;
  </label>
  <label for="q1b">
   <input type="radio" name="q1" value="b"  id="q1b"> B. False &nbsp;
  </label>
  <h3>2. Second question is either true or false.</h3>
  <label for="q1a">
   <input type="radio" name="q2" value="a" id="q1a"> A. True &nbsp;
  </label>
  <label for="q1b">
   <input type="radio" name="q2" value="b"  id="q1b"> B. False &nbsp;
  </label>
 <input class="btn btn-primary btn-lg" type="submit" name="submit" value="Submit!" onclick="return submitAnswers();" id="submission">

</form>

The script:-

function submitAnswers() {
  event.preventDefault();
    var total = 2;
    var score = 0;
    //captures each of the questions answers in a variable
    var q1 = document.form['javaScriptQuiz']['q1'].value;
    var q2 = document.form['javaScriptQuiz']['q2'].value;

    //Validation
    for(var i = 1; i <= total; i++ ){
        if(eval('q' + i) == null || eval('q' + i) == ''){
            alert('You missed a question');
            return false;
        }
    }

    // Are the answers to test
    var answers = ['b', 'a'];
    for(var i = 1; i <= total; i++){
        if(eval('q' + 1) == answers[i - 1] ){
            console.log('Evaluating answers');
            score ++;
        }
    }
}

Here's a working pen for the same.

1 Comment

This code I posted was an abbreviated version of what I originally created, I guess I missed something with the copy and paste. I can't believe I missed that. Thanks, for helping me find my errors though. :)
1

in your code, change

<input class="btn btn-primary btn-lg" type="submit" name="submit" value="Submit!" id="submission">

to

<input class="btn btn-primary btn-lg" type="button" name="submit" value="Submit!" id="submission"/>

or you can call the function itself in the input tag itself like onclick = "your_function_name()" . for your code, it will be:

<input class="btn btn-primary btn-lg" type="button" name="submit" value="Submit!" id="submission" onclick = "submitAnswers()"/>

Hope this helps..!!

1 Comment

Thanks I used your first suggestion. I'm still a little new to coding so can you tell me why 'button' is better than 'submit.' Just so I use it for future reference.
-1

solution1:change the type of your submit button to button

<input class="btn btn-primary btn-lg" type="button" name="submit" value="Submit!" id="submission">

solution2: change your on click event to form submit

$('#jsTest').on(submit,function(){
     ....});

Comments

-1

The default behavior for clicking an input with type="input" is to submit the form that the input is contained within.

Therefore if you do not want it to submit the form, you need to add a form submit handler and prevent the default behavior from happening:

$('#jsTest').on('submit', function(e) {
    e.preventDefault();
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.