335

I have 3 radio buttons in my web page, like below:

<label for="theme-grey">
 <input type="radio" id="theme-grey" name="theme" value="grey" />Grey</label>
<label for="theme-pink">
 <input type="radio" id="theme-pink" name="theme" value="pink" />Pink</label>
<label for="theme-green">
 <input type="radio" id="theme-green" name="theme" value="green" />Green</label>

In jQuery, I want to get the value of the selected radio button when any of these three are clicked. In jQuery we have id (#) and class (.) selectors, but what if I want to find a radio button by its name, as below?

$("<radiobutton name attribute>").click(function(){});

Please tell me how to solve this problem.

Scorpion-Prince
3,6443 gold badges21 silver badges24 bronze badges
asked Jun 12, 2009 at 11:10
5
  • 5
    you don't strictly need to specify the attribute 'for', as long as the fields are included between their corresponding 'label' tags Commented Sep 18, 2012 at 10:22
  • 2
    jQuery 1.8 and above changes this.... I added an answer below explaining. Commented Oct 3, 2012 at 17:02
  • 3
    A1rPun's answer is the best: a non-jQuery one-liner! Commented Sep 30, 2014 at 12:36
  • 1
    Used a radio button for maintaining state in my js app. Debugged for a good one hour before checking this thread. Check this out Commented Jun 5, 2015 at 5:41
  • In plain JS: document.querySelectorAll("input:radio[name=theme]").forEach(function() { this.onclick=function() { var value = this.value; }; }); Commented Apr 18, 2018 at 9:55

18 Answers 18

347

This should do it, all of this is in the documentation, which has a very similar example to this:

$("input[type='radio'][name='theme']").click(function() {
 var value = $(this).val();
});

I should also note you have multiple identical IDs in that snippet. This is invalid HTML. Use classes to group set of elements, not IDs, as they should be unique.

mbomb007
4,4113 gold badges52 silver badges79 bronze badges
answered Jun 12, 2009 at 11:15
Sign up to request clarification or add additional context in comments.

7 Comments

@gargantaun - if you click a radio button, what happens to it?
Good point. Although it's still not "How to get selected radiobutton value using its name in jQuery?". It's "How to get selected radiobutton value when clicking on it using jQuery?". A small difference, but one that baffled me for a bit.
According to the question this answer is correct. in global view we go for clayton's answer.
As of jQuery 1.8 use [type='radio'] instead of :radio that way jQuery can take advantage of the performance boost provided by the native DOM querySelectorAll() method.
@PaoloBergantino answer is 100% correct because it returns the value after clicking the desired radio button.@Clayton Rabenda answer does not give this.
|
187

To determine which radio button is checked, try this:

$('input:radio[name=theme]').click(function() {
 var val = $('input:radio[name=theme]:checked').val();
});

The event will be caught for all of the radio buttons in the group and the value of the selected button will be placed in val.

Update: After posting I decided that Paolo's answer above is better, since it uses one less DOM traversal. I am letting this answer stand since it shows how to get the selected element in a way that is cross-browser compatible.

answered Jun 12, 2009 at 15:06

4 Comments

Base don how I READ the question, this was the answer I needed. :checked is what i was missing in my equation. Thanks.
As of jQuery 1.8 use [type='radio'] instead of :radio that way jQuery can take advantage of the performance boost provided by the native DOM querySelectorAll() method.
I gave a space after colon and before "checked" by mistake. So please make sure there is no space.
A useful answer for those not wanting to get the value from a click event handler. Otherwise, you must use :checked to find which button is checked, for example, with a form submit event handler where the this keyword does not map to the clicked button.
157
$('input:radio[name=theme]:checked').val();
answered Jun 4, 2013 at 5:42

2 Comments

I went on simply with $("input[name=theme]:checked").val(); (leaving the :radio part out and it seems to work fine in IE, FF, Safari and Chrome. I had to work with jQ v 1.3... Of course, that means there is only one element named 'theme'
This is the best answer IMO, it says "get me the value of the CHECKED radio with THIS name". No need for events, etc.
39

another way

$('input:radio[name=theme]').filter(":checked").val()
answered Nov 30, 2013 at 5:34

2 Comments

This one is useful if you are interested in grabbing the value +1
This is solid because you can use a variable to keep the radio buttons, e.g. $themeRadios = $('input:radio[name=theme']) to set any event handlers and then get the value by using the filter, e.g. $themeRadios.filter(":checked").val().
20

This works great for me. For example you have two radio buttons with the same "name", and you just wanted to get the value of the checked one. You may try this one.

$valueOfTheCheckedRadio = $('[name=radioName]:checked').val();
answered Sep 7, 2012 at 17:38

1 Comment

The reason this answer is better than the currently-accepted 230+ upvoted answer is because this answer also accounts for when the user interacts with a radio button by keyboard.
16

The following code is used to get the selected radio button value by name

jQuery("input:radio[name=theme]:checked").val();

Thanks Adnan

LeonardChallis
7,8237 gold badges48 silver badges79 bronze badges
answered Mar 6, 2013 at 10:45

1 Comment

hmm .. didn't I fix your code formatting in your last reply? Please take care of it yourself :-)
14

For anyone who doesn't want to include a library to do something really simple:

document.querySelector('[name="theme"]:checked').value;

jsfiddle

For a performance overview of the current answers check here

answered Sep 30, 2014 at 12:34

1 Comment

Thanks for this answer. Because I was unable to get the accepted answer to work in any form.
13

I found this question as I was researching an error after I upgraded from 1.7.2 of jQuery to 1.8.2. I'm adding my answer because there has been a change in jQuery 1.8 and higher that changes how this question is answered now.

With jQuery 1.8 they have deprecated the pseudo-selectors like :radio, :checkbox, :text.

To do the above now just replace the :radio with [type=radio].

So your answer now becomes for all versions of jQuery 1.8 and above:

$("input[type=radio][name=theme]").click(function() { 
 var value = $(this).val(); 
}); 

You can read about the change on the 1.8 readme and the ticket specific for this change as well as a understand why on the :radio selector page under the Additional Information section.

answered Oct 3, 2012 at 17:02

1 Comment

:checked is not deprecated. So you can use $("input[type='radio'][name='theme']:checked")
9

If you'd like to know the value of the default selected radio button before a click event, try this:

alert($("input:radio:checked").val());
answered Jun 24, 2011 at 18:38

Comments

6

You can use filter function if you have more than one radio group on the page, as below

$('input[type=radio]').change(function(){
 var value = $(this).filter(':checked' ).val();
 alert(value);
});

Here is fiddle url

http://jsfiddle.net/h6ye7/67/

answered Mar 10, 2015 at 23:17

Comments

4
<input type="radio" name="ans3" value="help"> 
<input type="radio" name="ans3" value="help1">
<input type="radio" name="ans3" value="help2">
<input type="radio" name="ans2" value="test"> 
<input type="radio" name="ans2" value="test1">
<input type="radio" name="ans2" value="test2">
<script type="text/javascript">
 var ans3 = jq("input[name='ans3']:checked").val()
 var ans2 = jq("input[name='ans2']:checked").val()
</script>
answered Jul 4, 2012 at 6:33

Comments

4

If you want a true/false value, use this:

 $("input:radio[name=theme]").is(":checked")
answered Aug 5, 2013 at 16:17

Comments

3

Something like this maybe?

$("input:radio[name=theme]").click(function() { 
 ...
}); 

When you click on any radio button, I believe it will end up selected, so this is going to be called for the selected radio button.

answered Jun 12, 2009 at 11:13

1 Comment

The @ is invalid as of jQuery 1.3 (deprecated prior to that, even). blog.jquery.com/2009/01/05/help-test-jquery-13-beta-2 "Old, XPath, style attribute selectors: [@attr=value]. These have been deprecated for quite some time - and we’re finally removing them. To fix it just remove the @!"
3

I you have more than one group of radio buttons on the same page you can also try this to get the value of radio button:

$("input:radio[type=radio]").click(function() {
 var value = $(this).val();
 alert(value);
});

Cheers!

Jonnny
5,05912 gold badges67 silver badges98 bronze badges
answered Jan 14, 2015 at 18:01

Comments

2

can also use a CSS class to define the range of radio buttons and then use the following to determine the value

$('.radio_check:checked').val()
answered Jun 14, 2012 at 16:37

Comments

1

This worked for me..

HTML:

<input type="radio" class="radioClass" name="radioName" value="1" />Test<br/>
<input type="radio" class="radioClass" name="radioName" value="2" />Practice<br/>
<input type="radio" class="radioClass" name="radioName" value="3" />Both<br/>

Jquery:

 $(".radioClass").each(function() {
 if($(this).is(':checked'))
 alert($(this).val());
 });

Hope it helps..

answered Feb 26, 2013 at 8:11

Comments

0
$('input:radio[name=theme]').bind(
 'click',
 function(){
 $(this).val();
});
bluish
27.6k28 gold badges126 silver badges185 bronze badges
answered Jun 12, 2009 at 18:58

Comments

0

You might notice using class selector to get value of ASP.NET RadioButton controls is always empty and here is the reason.

You create RadioButton control in ASP.NET as below:

<asp:RadioButton runat="server" ID="rbSingle" GroupName="Type" CssClass="radios" Text="Single" />
<asp:RadioButton runat="server" ID="rbDouble" GroupName="Type" CssClass="radios" Text="Double" />
<asp:RadioButton runat="server" ID="rbTriple" GroupName="Type" CssClass="radios" Text="Triple" />

And ASP.NET renders following HTML for your RadioButton

<span class="radios"><input id="Content_rbSingle" type="radio" name="ctl00$Content$Type" value="rbSingle" /><label for="Content_rbSingle">Single</label></span>
<span class="radios"><input id="Content_rbDouble" type="radio" name="ctl00$Content$Type" value="rbDouble" /><label for="Content_rbDouble">Double</label></span>
<span class="radios"><input id="Content_rbTriple" type="radio" name="ctl00$Content$Type" value="rbTriple" /><label for="Content_rbTriple">Triple</label></span>

For ASP.NET we don't want to use RadioButton control name or id because they can change for any reason out of user's hand (change in container name, form name, usercontrol name, ...) as you can see in code above.

The only remaining feasible way to get the value of the RadioButton using jQuery is using css class as mentioned in this answer to a totally unrelated question as following

$('span.radios input:radio').click(function() {
 var value = $(this).val();
});
answered Jun 19, 2015 at 3:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.