Go to DotNetFunda.com
  Welcome, Guest!  
LoginLogin  
{ Submit resources and get monthly gifts !!! }
Submit: Article | Interview Question | Joke | Question | Link || Search  
 Skip Navigation Links Home > Articles > Getting selected value from asp:RadioButtonList in JavaScript

All Articles | Post Articles

Getting selected value from asp:RadioButtonList in JavaScript

 Posted on: 4/15/2008 4:03:49 AM by Raja | Views: 1434 | Category: JavaScript | Level: Intermediate | Print Article
While writing UI code, we must have came across scenaio where we need to get the asp:RadioButtonList value into JavaScript and validate some other field.
HTML Code

In following code I am getting asp:RadioButtonList and populating 3 ListItems. Then I have a button and I am calling a JavaScript function when it is clicked.


- Hide Code
<table>

<tr>
<td>
<asp:RadioButtonList ID="radiobuttonlist1" runat="Server" RepeatLayout="flow" RepeatDirection="horizontal">
<asp:ListItem Text="Radio 1" Value="1" Selected="True"></asp:ListItem>
<asp:ListItem Text="Radio 2" Value="2"></asp:ListItem>
<asp:ListItem Text="Radio 3" Value="3"></asp:ListItem>
</asp:RadioButtonList>
</td>
<td>
<input type="button" value="Submit" onclick="GetRadioButtonValue('<%= radiobuttonlist1.ClientID %>')" />
</td>
</tr>
</table>

JavaScript Code

In following code, I am getting the radio button element looping through its length. Checking each radiobutton for its checked value by accessing thorugh its index. As I am using RadioButtonList and I have 3 ListItem so I will collection of RadioButtons that lets me get each one of them by its index. If the radio[j].checked is true and I am giving value into an alert box.



- Hide Code
<script language="javascript" type="text/javascript">

// Get radio button list value
function GetRadioButtonValue(id)
{
var radio = document.getElementsByName(id);
for (var j = 0; j < radio.length; j++)
{
if (radio[j].checked)
alert(radio[j].value);
}
}
</script>



I hope this small article help someone in need :) Happy Coding !!

Bookmark and Share

About Raja Dutta

Experience:3 year(s)
Home page:
Member since:Monday, June 02, 2008
Biography:--
Raja
 Latest post(s) from Raja

   ◘ Forms Authentication in ASP.NET with C# posted on 7/30/2008 4:08:34 PM
   ◘ Getting selected value from asp:RadioButtonList in JavaScript posted on 4/15/2008 4:03:49 AM
   ◘ Reading appSettings and connectionStrings from web.config file in asp.net posted on 10/9/2007 7:14:12 PM
   ◘ Generating Ms Word document in ASP.NET and C# posted on 9/11/2007 4:49:45 PM
   ◘ Dynamically writing Meta tags on Master page from a User Control or any other class file posted on 6/21/2007 2:29:26 PM


Question: Why to use www.dotnetfunda.com google search?
Answer: This search has been especially optimized to search technical articles. You may find to-the-point results in comparison with other search.
Google
About Us | Contact Us | Privacy Policy and Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
This site is best viewed with a resolution of 1280x720 (or higher) and Microsoft Internet Explorer 6.0+ or Firefox 2.0+.