radio box input doesn’t store the value instead shows "on" in the database
here is my code.
<label for="gender">Female</label>
<input type="radio" name="gender">
<label for="gender">Male</label>
<input type="radio" name="gender">
<label for="gender">Transgender</label>
<input type="radio" name="gender">
<span class="formerror"></span>
this is my schema
gender: {
type: String,
required: true
}
this is the data in the database stored
{
name: 'Ethan Hunt',
age: 60,
gender: 'on',
blood_group: 'B+',
date: 2023-07-05T00:00:00.000Z,
phone_number: '7074788341',
medical_history: 'cataract',
password: '1234',
_id: new ObjectId("64a1bced4796f50508c67030"),
__v: 0
}
i was expecting the gender value to show up as "male" "female" or "transgender".
i also tried adding
gender: {
type: String,
enum: ["female", "male", "transgender"],
required: true
},
but it generates an error
"Detail validation failed: gender: on
is not a valid enum value for path gender
.
at ValidationError"
2
Answers
give your html element a "value" attribute and set it’s value to the gender value it denotes.
also read: https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
for more insight on accessing the value via javaScript
You need to give each option/radio button a value. And now you can ask for the value on the name (
gender
). Thee.target
in the submit event callback is the form element.The
for
attribute on<label>
only work for IDs. Skip the IDs and have the input as a child to label will work fine.