Get RadioButtonList SelectedValue in JavaScript

Scenario: There will be times when you would like to access the selected value of your RadioButtonList on the client-side via JavaScript. One of those time coulde be you want to hide/show something elements on your page based on the radio button option selected. Another time could be you want to open a popup window and pass this value to a page via querystring. So the code sample below will is what you can use to get the selected value of the RadioButonList and then use is per your need.

This is nothing really new but a very old concept. I was lying around in one of my testsite and recently I found someone asking the same question on one of the forum. So I thought to put this on here.

Code:

  JavaScipt

RadioButtonList

 
            One
            Two
            Three
            Four
        

Brief Explanation:

The RadioButtonList has four items and and "onclick" event that will call the rblSelectedValue javascript function.

In the rblSelectedValue() function, first we get the reference to our RadioButtonList1.

 

     var radio = document.getElementsByName('<%= RadioButtonList1.ClientID %>');

 

Note we are not using 'RadioButtonList1" as a parameter to getElementsByName method but using a special syntax to the the ClientID for the RBL1, which is generated by asp.net framework.

Then we loop through the items in the RBL using for loop. Note the radio.length, which gives the the number of items in the radiobuttonlist. In the loop we check if the individual item is checked and if it is then assign the value of that item to our local variable, selectedValue.

Now we can use this value as per our need. As shown in the example, we have an alert as a debug statement to check if we are getting right value and then we are passing this value as a querystring parameter to a url and open that url in a new popup window using window.open method.

Update (04/25/2011):

Here is code to get selected value of RadioButtonList using jQuery. The two different ways are more generic i.e. if you have multiple radiobuttonlist, you don’t have to replicate js code for each of them.

1: Adding onclick event directly to RadioButtoList:

function rblSelectedValue(rbl) {
var selectedvalue = $("#" + rbl.id + " input:radio:checked").val();
alert(selectedvalue);
}

<asp:RadioButtonList ID="RadioButtonList1" runat="server" OnClick="rblSelectedValue(this)">

2: Add identical CssClass to each RadioButtonList:
$(function () {
$(".myrbl").click(function () {
var selectedvalue = $("#" + $(this).attr('id') + " input:radio:checked").val();
alert(selectedvalue);
});
});

<asp:RadioButtonList ID="RadioButtonList1" runat="server" CssClass="myrbl">

Conclusion:

Using pure JavaScript is one way. We can use jQuery to achieve the same goal. Let me know if anyone is looking for a jQuery sample.

References:

Published Thursday, October 14, 2010 1:32 PM by guru_sarkar

Comments

# re: Get RadioButtonList SelectedValue in JavaScript

Saturday, April 23, 2011 4:23 AM by ID

Thanks a lot for this code.

What about when there are more than 1 radiobuttonlist and I don't would like to use just one function for all by passing the radio button id and the ID of the div element to hide/show as parameters?

Also it would be nice to see the jquery version.

Cheers.

# re: Get RadioButtonList SelectedValue in JavaScript

Monday, April 25, 2011 12:57 PM by guru_sarkar

@ID,

I have updated the post to have more generic function and it uses jQuery.

# re: Get RadioButtonList SelectedValue in JavaScript

Monday, August 22, 2011 10:46 PM by dinhsac

thanks

# re: Get RadioButtonList SelectedValue in JavaScript

Friday, September 16, 2011 10:05 AM by gotibandhu

Re:Get RadioButtonList SelectedValue in JavaScript

Hi Author,

Great article!!!!

Here you may check out a nice article about RadioButtonList SelectedValue in JavaScript by following URL.

www.mindstick.com/.../Get%20checked%20radiobutton%20using%20JavaScript

Thanks!!!!!

# re: Get RadioButtonList SelectedValue in JavaScript

Friday, October 14, 2011 6:42 PM by Dilip

Jquery link How-to-set-get-radiobuttonlist-selected Value worked

# re: Get RadioButtonList SelectedValue in JavaScript

Friday, January 20, 2012 9:16 AM by Vaibhav

Thanx its working

# re: Get RadioButtonList SelectedValue in JavaScript

Thursday, March 29, 2012 3:44 AM by Shamjith

Nice article

# re: Get RadioButtonList SelectedValue in JavaScript

Friday, April 06, 2012 9:18 AM by Bidhu

The above code written is not working as expected. The length of radiobutton list is always comes as 0 which is not expected. So, plz rectify it.

# re: Get RadioButtonList SelectedValue in JavaScript

Tuesday, April 17, 2012 10:09 AM by Sandeep Augustine (sapien4u@gmail.com)

Hi, the jquery code really helped me a lot.. thanks :)

# re: Get RadioButtonList SelectedValue in JavaScript

Thursday, July 05, 2012 9:19 AM by anwer jamal

thanks, it is working nicely.

# re: Get RadioButtonList SelectedValue in JavaScript

Tuesday, July 10, 2012 2:46 PM by Jason

You can't use ClientID, you have to use UniqueID.  The names of the inputs are different than their ID's.

# re: Get RadioButtonList SelectedValue in JavaScript

Monday, August 20, 2012 1:38 AM by Razvan Valcaneantu

with jquery:

$("input[name$='radioName']:checked").val()

# re: Get RadioButtonList SelectedValue in JavaScript

Wednesday, October 10, 2012 5:55 AM by SaiPrashad

I tried but the above code is not working

# re: Get RadioButtonList SelectedValue in JavaScript

Thursday, November 29, 2012 1:02 PM by wunderkinder

Just the code I was looking for. Thanks!

Leave a Comment

(required) 
(required) 
(optional)
(required)