jmona789 jmona789 - 4 months ago 109
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:



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


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

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"}