oliboon
oliboon

Reputation: 361

Get value of multiple choices checkboxes Javascript

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.

Upvotes: 0

Views: 2020

Answers (1)

Quentin
Quentin

Reputation: 944545

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.

Upvotes: 1

Related Questions