Question
Get the Selected Radio Button Value with jQuery
Question
I have two radio buttons in a form, and I want to submit the value of the one the user selected.
How can I get the selected value using jQuery?
I can currently select all radio buttons like this:
$("form :radio")
How do I determine which radio button is currently selected?
Short Answer
By the end of this page, you will understand how radio buttons work in HTML forms, how jQuery can find the checked radio button, and how to safely read its value before sending it in a form submission or AJAX request.
Concept
Radio buttons are used when a user must choose one option from a group. In HTML, radio buttons belong to the same group when they share the same name attribute.
Only one radio button in a group can be selected at a time. The selected radio button is the one with the checked state.
In jQuery, the most common way to find the selected radio button is to use the :checked selector.
For example:
$('input[name="choice"]:checked').val()
This works because:
input[name="choice"]finds all radio buttons in the group:checkednarrows that set down to the currently selected one.val()reads itsvalue
This matters in real programming because radio buttons are often used for:
- payment method selection
- shipping options
- user preferences
- filters in search forms
- survey answers
If you do not target the checked radio button, you may read the wrong value or accidentally work with all radio buttons instead of the selected one.
Mental Model
Think of a radio button group like a set of labeled switches where only one switch can stay on.
- The
nameattribute defines the group - Each radio button has a
value :checkedasks: which switch is currently on?
So instead of asking for all switches, you ask for the switch that is on right now.
Syntax and Examples
Basic syntax
$('input[name="myRadioGroup"]:checked').val()
Example HTML
<form id="myForm">
<label>
<input type="radio" name="color" value="red"> Red
</label>
<label>
<input type="radio" name="color" value="blue"> Blue
</label>
</form>
Get the selected value
var selectedValue = $('input[name="color"]:checked').val();
console.log(selectedValue);
If Red is selected, this prints:
Step by Step Execution
Traceable example
<form id="settingsForm">
<input type="radio" name="theme" value="light">
<input type="radio" name="theme" value="dark" checked>
</form>
var selected = $('#settingsForm input[name="theme"]:checked');
var value = selected.val();
console.log(value);
What happens step by step
-
$('#settingsForm input[name="theme"]:checked')- jQuery looks inside the form with id
settingsForm - It finds radio inputs with
name="theme" - It filters them to only the one that is
:checked
- jQuery looks inside the form with id
Real World Use Cases
Radio button selection appears in many common features:
- Checkout forms: choose standard or express shipping
- Payment selection: choose card, PayPal, or bank transfer
- User settings: pick light or dark theme
- Polls and surveys: select one answer from several choices
- Search filters: choose sort order such as newest or cheapest
- Admin panels: choose status like active or inactive
Example: sending the selected shipping option in an AJAX request:
var shipping = $('input[name="shipping"]:checked').val();
$.post('/checkout', {
shippingMethod: shipping
});
This makes sure only the user's actual choice is submitted.
Real Codebase Usage
In real projects, developers usually combine selected radio values with validation and event handling.
Common patterns
Guard clause before using the value
var paymentMethod = $('input[name="payment"]:checked').val();
if (!paymentMethod) {
alert('Please select a payment method.');
return;
}
This avoids sending empty or invalid form data.
Read the value when the user changes selection
$('input[name="payment"]').on('change', function () {
console.log('Selected:', $(this).val());
});
Here, $(this) refers to the radio button the user just selected.
Use the value during form submission
$('#checkoutForm').on('submit', function (e) {
e.preventDefault();
method = $().();
(!method) {
();
;
}
.(, method);
});
Common Mistakes
1. Selecting all radio buttons instead of the checked one
Broken example:
$('form :radio').val();
Why it is a problem:
- This selects all radio buttons in the form
.val()may return only the first matched element's value, not the selected one
Correct version:
$('form input[type="radio"]:checked').val();
Or better, target the group by name:
$('input[name="color"]:checked').val();
2. Forgetting that radio buttons must share the same name
Broken HTML:
<input type="radio" name="color1" value="red">
<input type="radio" name="color2" =>
Comparisons
Related selector choices
| Approach | What it does | Good use case | Notes |
|---|---|---|---|
$('input[name="color"]:checked').val() | Gets the selected radio value in a group | Best general solution | Clear and specific |
$('form :radio:checked').val() | Gets the checked radio inside a form | Good if only one radio group matters | Less specific than using name |
$(this).val() inside change handler | Gets the value of the radio just selected | Best in event-driven code | Useful when reacting immediately |
$('form :radio').val() | Gets value from matched radio elements |
Cheat Sheet
// Get selected radio value by group name
$('input[name="groupName"]:checked').val();
// Example
$('input[name="color"]:checked').val();
// Inside a specific form
$('#myForm input[name="color"]:checked').val();
// In a change event
$('input[name="color"]').on('change', function () {
console.log($(this).val());
});
Rules to remember
- Radio buttons in the same group must share the same
name - Use
:checkedto find the selected one - Use
.val()to get its value - If none is selected,
.val()returnsundefined - Prefer specific selectors like
input[name="..."]over broad ones like:radio
Common pattern
FAQ
How do I get the selected radio button value in jQuery?
Use:
$('input[name="yourGroupName"]:checked').val();
Why does $('form :radio').val() not give the selected value?
Because it selects all radio buttons, not just the checked one. Add :checked to target the selected radio button.
What happens if no radio button is selected?
.val() returns undefined, so you should check for that before using the result.
Can I get the selected radio button when the user clicks it?
Yes. Use a change event:
$('input[name="color"]').on('change', function () {
console.log($(this).val());
});
Do radio buttons need the same name?
Yes. Radio buttons only behave as one group if they share the same name attribute.
Can I use for checkboxes too?
Mini Project
Description
Build a small preference form where a user chooses a notification method using radio buttons and then submits the selected choice. This demonstrates how to read the checked radio button value and validate that the user made a selection before continuing.
Goal
Create a form that reads the selected radio button value with jQuery and displays it to the user.
Requirements
- Create a form with one group of radio buttons for notification method
- Add a submit button
- Use jQuery to read the selected radio button when the form is submitted
- Show a message if no option is selected
- Display the selected value if an option is chosen
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers.
HTML id Attribute Rules: Valid Values and Best Practices
Learn which HTML id values are valid, how browsers treat them, and the best practices for naming ids in modern HTML and CSS.