0

I'm using the onsubmit event on a form to verify it before it is sent. I'm having issues getting the value of checkboxes that allows multiple choice.

Html:

<input type="checkbox" name="question5[]" value="1" />
<input type="checkbox" name="question5[]" value="2" /> 

Javascript:

var form = document.forms['questionnaire'];
var q5 = form.elements["question5"].value;

When I try to get the value of this question I'm not able to retrieve it the same way I did for the other fields. I'm wondering what is the correct way to get the value of those checkboxes since I can't retrieve it like a radio or text input.

1 Answer 1

1

The name of the field is question5[] not question5 and since you have multiple of them, you will get a NodeList (which is like an Array) back, not a single Element.

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

1 Comment

I tried var q5 = form.elements["question5"][0].value; and var q5 = form.elements["question5[]"].value; but I didn't try to combine those. It works now.

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.