333

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.

5
  • 5
    you don't strictly need to specify the attribute 'for', as long as the fields are included between their corresponding 'label' tags
    – Lucius
    Sep 18, 2012 at 10:22
  • 2
    jQuery 1.8 and above changes this.... I added an answer below explaining. Oct 3, 2012 at 17:02
  • 3
    A1rPun's answer is the best: a non-jQuery one-liner!
    – Rudey
    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
    – Prasanth
    Jun 5, 2015 at 5:41
  • In plain JS: document.querySelectorAll("input:radio[name=theme]").forEach(function() { this.onclick=function() { var value = this.value; }; });
    – mplungjan
    Apr 18, 2018 at 9:55

18 Answers 18

346

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.

7
  • 4
    @gargantaun - if you click a radio button, what happens to it? Mar 2, 2012 at 0:23
  • 11
    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.
    – gargantuan
    Mar 2, 2012 at 10:13
  • 1
    According to the question this answer is correct. in global view we go for clayton's answer.
    – Krish
    May 27, 2012 at 5:32
  • 8
    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.
    – Adam
    Oct 28, 2012 at 18:01
  • 2
    @PaoloBergantino answer is 100% correct because it returns the value after clicking the desired radio button.@Clayton Rabenda answer does not give this.
    – Azam Alvi
    Aug 4, 2013 at 10:25
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.

4
  • 3
    Base don how I READ the question, this was the answer I needed. :checked is what i was missing in my equation. Thanks.
    – HPWD
    Oct 25, 2012 at 15:00
  • 9
    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.
    – Adam
    Oct 28, 2012 at 18:02
  • I gave a space after colon and before "checked" by mistake. So please make sure there is no space. Sep 26, 2014 at 6:27
  • 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. Jan 3, 2015 at 22:30
156
$('input:radio[name=theme]:checked').val();
2
  • 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' Feb 14, 2014 at 19:57
  • 2
    This is the best answer IMO, it says "get me the value of the CHECKED radio with THIS name". No need for events, etc.
    – ProVega
    Mar 20, 2015 at 16:52
39

another way

$('input:radio[name=theme]').filter(":checked").val()
2
  • 1
    This one is useful if you are interested in grabbing the value +1
    – swapnesh
    Jan 27, 2014 at 12:22
  • 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(). Jan 29, 2015 at 16:35
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();
1
  • 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.
    – user110857
    Apr 14, 2015 at 18:47
16

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

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

Thanks Adnan

1
  • hmm .. didn't I fix your code formatting in your last reply? Please take care of it yourself :-)
    – kleopatra
    Mar 6, 2013 at 10:56
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

1
  • 1
    Thanks for this answer. Because I was unable to get the accepted answer to work in any form. Apr 26, 2017 at 14:51
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.

1
  • 6
    :checked is not deprecated. So you can use $("input[type='radio'][name='theme']:checked")
    – Adam
    Oct 28, 2012 at 17:59
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());
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/

0
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>
4

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

  $("input:radio[name=theme]").is(":checked")
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.

1
  • 1
    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 @!"
    – racerror
    Jun 12, 2009 at 22:09
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!

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()
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..

0
$('input:radio[name=theme]').bind(
  'click',
  function(){
    $(this).val();
});
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();
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.