jmona789 jmona789 - 1 year ago 228
Node.js Question

How can I tell which radio button is selected with node.js

I have a form with two radio button and I'm using posting the request and using the body parser npm package to see the values of the form inputs, the only problem is I have two radio buttons on the form like this:

<input class="with-gap" name="group1" type="radio" id="test1" />
<label for="test1">Teacher</label>
<input class="with-gap" name="group1" type="radio" id="test2" />
<label for="test2">TA</label>

The only problem is when I console.log req.body the only info on the radio buttons I get is:

{ group1: 'on' }

It doesn't tell me which radio button was clicked and I change one of the names to group2 it allows the user to click both radio buttons, so how can I determine which radio button was clicked?

Answer Source

You should give the radio-buttons a attribute of value. Like:

<input class="with-gap" name="group1" type="radio" id="test1" value="test1"/>

It should make the object from body-parser read {group1:"test1"}