Click here to Skip to main content
15,791,229 members
Articles / Programming Languages / Javascript

Radio Buttons, Groups and JQuery

Rate me:
Please Sign up or sign in to vote.
4.89/5 (7 votes)
18 Jul 2011CPOL 56.7K   5   5
Simple implementation for determining which radio buttons are selected using Jquery
Recently I was working on a small tool/article and was trying to work out the simplest way of handling RadioButtons, Radio Button Groups and then determining which is selected using JQuery.

This is the implementation I came up with;

1) Add your radio buttons to the page and assign them their unique ID as usual
2) For each radio button in the group, set the NAME property to the SAME value
3) Assign a unique value to each radio button VALUE property

What you will end up with is something like the following (radio button 1 is default selected);

<input type="radio" id="radio1" name="RadioGroup1" value="1" checked="true"/>
<input type="radio" id="radio2" name="RadioGroup1" value="2" />
<input type="radio" id="radio3" name="RadioGroup1" value="3" />

By assigning the same NAME to each radio button means that if radio2 is selected, then radio1 and radio3 are deselected. If you don't set the same name, then multiple radio buttons can become selected.

Then to determine which radio button is selected using JQuery you can use the following code;
var value = $("input[name=RadioGroup1]:checked").val();

alert("The user selected; " + value);

The JQuery statement, looks for an input element which has the name property "RadioGroup1" and is checked, then returns its value.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Scotland Scotland
I have been working in the Oil & Gas Industry for over 30 years now.

Core Discipline is Instrumentation and Control Systems.

Completed Bsc Honours Degree (B29 in Computing) with the Open University in 2012.

Currently, Offshore Installation Manager in the Al Shaheen oil field, which is located off the coast of Qatar. Prior to this, 25 years of North Sea Oil & Gas experience.

Comments and Discussions

GeneralMy vote of 5 Pin
David A. Gray22-Aug-22 11:55
David A. Gray22-Aug-22 11:55 
This deserves way more 5-star votes than it has accumulated over the last eleven years. The examples are clear, simple, and on point. This article was the first result returned by my Bing search, and I need go no further down the page of results.
GeneralReason for my vote of 4 Very simple and straight forward. A... Pin
BrianBissell26-Jul-11 4:59
BrianBissell26-Jul-11 4:59 
GeneralRe: Hi Brian, thanks for the feedback, but what is missing about... Pin
DaveAuld26-Jul-11 5:42
professionalDaveAuld26-Jul-11 5:42 
GeneralThanks, I have corrected my error in the code, thanks for po... Pin
DaveAuld19-Jul-11 0:10
professionalDaveAuld19-Jul-11 0:10 
GeneralYou don't need (or must not) use '@' before the attribute na... Pin
dblaci19-Jul-11 0:07
dblaci19-Jul-11 0:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.