I have a problem, I want to validate a input type="radio"
with vanilla JavaScript (probably using document.getElementById()
), based on ECMAScript. How do I do it?
Here is my HTML:
<div class="gender-input">
<input type="radio" name="gender" id="male">
<label for="male">Male</label>
</div>
<div class="gender-input">
<input type="radio" name="gender" id="female">
<label for="others">Female</label>
</div>
I don’t know how to create a function to verify if the radio button is checked or not. I saw some posts talking about this, but all of them used some frameworks or another solutions, but I just want to use Vanilla JavaScript.
2
Answers
Did you try something like:
Or
You can use
querySelector
, Becase the are same name. which one checked we can find it。 then we retrieve the id or get value