Click here to Skip to main content
Click here to Skip to main content

Tagged as

Radio Buttons, Groups and JQuery

, 18 Jul 2011
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

DaveAuld
Engineer
Scotland Scotland
I have been working in the Oil & Gas Industry for over 20 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 for the Beryl Bravo platform, which is located ~180 miles NE of Aberdeen, Scotland in the Northern North Sea.
Formely on the Forties Charlie platform, which is located ~110Miles NE of Aberdeen.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralReason for my vote of 4 Very simple and straight forward. A... PinmemberBrianBissell26-Jul-11 3:59 
GeneralRe: Hi Brian, thanks for the feedback, but what is missing about... PinmentorDaveAuld26-Jul-11 4:42 
GeneralThanks, I have corrected my error in the code, thanks for po... PinmentorDaveAuld18-Jul-11 23:10 
GeneralYou don't need (or must not) use '@' before the attribute na... Pinmemberdblaci18-Jul-11 23:07 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 19 Jul 2011
Article Copyright 2011 by DaveAuld
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid