How to check a radio button with jQuery?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In jQuery, the correct way to check a radio button is to set its checked property, not to manipulate the old HTML attribute directly. Because radio buttons belong to a named group, checking one option also unchecks the others in that same group automatically. Once you understand that browser behavior, the jQuery solution becomes very simple.
Use .prop("checked", true)
The standard jQuery approach is:
Given HTML like this:
that line checks the radio button with id blue. The browser automatically clears any other checked radio button in the same name="color" group.
Select by Name and Value When There Is No ID
If the radio buttons do not have unique IDs, you can target them by group name and value:
This is often the most practical selector when the choice comes from saved application state or a server response.
Example with dynamic data:
That is a clean pattern for restoring form state.
Why .prop() Is Better Than .attr()
For boolean DOM state such as checked, disabled, and selected, jQuery's .prop() is the correct modern method.
The older .attr("checked", "checked") style changes the HTML attribute, but the live checked state of form controls is better represented as a DOM property.
If you are maintaining old jQuery code, updating radio-button logic from .attr() to .prop() is usually the right move.
Trigger Change Logic When Needed
Setting the property does not always guarantee that code listening for user changes will run automatically. If other logic depends on the radio button's change event, trigger it explicitly:
This is useful when:
- showing or hiding related fields
- recalculating a form summary
- syncing the UI with a saved preference
Be careful not to confuse programmatic state updates with real user interaction. If the rest of the app depends on the change event, trigger it deliberately.
Example with a Button Click
A common pattern is checking a radio button in response to another action:
This keeps the code readable and makes the resulting form state explicit.
Clearing or Replacing the Selection
Because radio buttons represent a single choice within a group, you usually do not manually uncheck the rest of the group. Checking one item handles that automatically.
If you truly need to clear all radios in a group, that is less natural in plain HTML because radio groups are designed to have at most one selected item, not necessarily to return to a no-selection state after interaction. It is possible, but it should usually be a deliberate UX decision rather than the default behavior.
Common Pitfalls
One common mistake is using .attr("checked", true) instead of .prop("checked", true). The property is the live state that form controls actually use.
Another mistake is selecting by id when the real requirement is selecting by group name and value. For radio groups restored from saved data, name and value selectors are often more robust.
Developers also sometimes expect application logic listening for changes to run automatically after a programmatic update. If that logic depends on a change handler, trigger it explicitly.
Finally, remember that the grouping behavior comes from the shared name attribute. If your radios do not share the same name, checking one will not uncheck the others because they are not actually in the same group.
Summary
- In jQuery, check a radio button with
.prop("checked", true). - Use an id selector when you know the specific element.
- Use a
nameplusvalueselector when the choice is data-driven. - Trigger
changemanually if other code depends on it. - Make sure related radio buttons share the same
nameso the group behaves correctly.

