# How do I get the value of the selected radio button in JavaScript

Source: https://coursesweb.net/forum/t/how-do-i-get-the-value-of-the-selected-radio-button-in-javascript/
Site: CoursesWeb Forum, category JavaScript and the DOM
Published: 2026-11-30. Updated: 2026-12-01. Replies: 1.

## Question by Odalys F., 2026-11-30

I am building a tiny quiz page for my coding club. Each question has three radio buttons and a Check button. I tried this:

```html
<input type="radio" name="q1" value="paris"> Paris
<input type="radio" name="q1" value="rome"> Rome
<input type="radio" name="q1" value="madrid"> Madrid
<button id="check">Check</button>
```

```js
document.querySelector("#check").addEventListener("click", () => {
  const answer = document.getElementsByName("q1").value;
  console.log(answer);
});
```

The console prints `undefined` whatever I pick. One of my students said "loop through them", but that seems like a lot of code for twelve year olds. Is there a short, readable way to get the one that is ticked, and what should happen when nobody has picked anything yet?

## Reply 1 by Kaveh S., 2026-12-01

getElementsByName gives you a list, not one input, that's why .value is undefined. I hit the same thing with getElementsByClassName last month.

---
Cite as: CoursesWeb Forum, "How do I get the value of the selected radio button in JavaScript", https://coursesweb.net/forum/t/how-do-i-get-the-value-of-the-selected-radio-button-in-javascript/, accessed 2026-12-01.
