Checking Value of Radio Button Group via JavaScript?

Propeller picture Propeller · Mar 5, 2012 · Viewed 187.6k times · Source

This may seem silly and downright stupid but I can't seem to figure out how to check the value of a radio button group in my HTML form via JavaScript. I have the following code:

<input type="radio" id="genderm" name="gender" value="male" />
<label for="genderm">Male</label>
<input type="radio" id="genderf" name="gender" value="female" />
<label for="genderf">Female</label>

How do I retrieve the value of gender via JavaScript?

Answer

darelf picture darelf · Jun 18, 2015

Use document.querySelector() if you want to avoid frameworks (which I almost always want to do).

document.querySelector('input[name="gender"]:checked').value