- Instant help with your JavaScript coding problems

Get the value of selected radio button in JavaScript

Question:
How to get the value of selected radio button in JavaScript?
Answer:
const selectedColor = document.querySelector('input[name="color"]:checked').value;
Description:

Even if HTML radio buttons look a bit different compared to a simple text input, getting the selected value is the same. You need to read the value property of the selected element. So the only difficulty is to get the right element. To do this you can use the querySelector method with a selector that selects only the checked value from the radio items.

The code above reads the value from the following radio buttons:

<input type="radio" name="color" value="1"> Red
<input type="radio" name="color" value="2"> Green
<input type="radio" name="color" value="3" checked="checked"> Blue  

 

Share "How to get the value of selected radio button in JavaScript?"