2

I'm doing an assignment with JavaScript and I have to make a quiz. My problem is I'm using the HTML 5 "required attribute" on my radio buttons. The problem is when I use an onclick event for the submit button it disregards the required attribute and executes the function to display a new question/choices. So if the radio buttons aren't selected it still performs the onclick event.

I'd like it so the required attribute checks if the radio buttons are checked before the onclick event.

I'm just not sure how to do this.

I display my radio buttons like this

  var radio = document.createElement("input");
    radio.type = "radio";
    radio.id = i;
    radio.value = i;
    radio.name = "option";
    radio.required = true;

Here is my JsFiddle: http://jsfiddle.net/VodkaTonic/4FJmU/

2
  • Are you allowed to use JQuery... Commented Feb 23, 2014 at 4:10
  • 1
    Yes I can, but I'd rather learn by using pure JS. Commented Feb 23, 2014 at 4:14

3 Answers 3

2

Your problem is that you are binding an onClick method to the submit. The onClick will trigger and process BEFORE the onSubmit, which results in it getting past the validator. Try changing the onclick to onsubmit like this:

nextButton.onsubmit = function() {
    checkAnswer(currentQuestion);
}

Fiddle

Note, you will probably need to cancel the actual form submit, since you aren't sending any data anywhere.

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

Comments

1

You attached an onclick event to the go to the next question, form validation is only preformed on submit. Instead attach it to the onsubmit event of the form:

quiz.onsubmit = function(e) {
    e.preventDefault();
    checkAnswer(currentQuestion);
}

A couple of other things...

CSS identifiers such as id can not start with a digit, you need to prefix it with a letter:

radio.id = 'r' + i;

(you'll also need to update them in the getElementById calls in checkAnswer)

Your labels don't work correctly either. for is a reserved word in JavaScript. Unfortunately reserved words can't even be used as dot properties of objects (if you are making your own object you could set/get it using the bracket notation obj["for"], but not dot notation obj.for). For this reason the for attribute of DOM nodes is accessed via .htmlFor:

    label.htmlFor = radio.id;

There are a few others like this to watch out for, the most common of which would e .className to access the class attribute of DOM nodes.

Fiddle with these fixes

Comments

1

WORKING

http://jsfiddle.net/rnrlabs/4FJmU/3/

you must use FORM.onsubmit

/*nextButton.onsubmit = function() {
      checkAnswer(currentQuestion);
}*/

quiz.onsubmit = function() {
      checkAnswer(currentQuestion);
      return false;
}

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.