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.





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 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 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 = $("#" + + " input:radio:checked").val();

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

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


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.



Comments have been disabled for this content.