Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 35587 |  Welcome, Guest!   Register  Login
Home > Articles > JavaScript > Getting selected value from asp:RadioButtonList in JavaScript

Getting selected value from asp:RadioButtonList in JavaScript

4 vote(s)
Rating: 2.25 out of 5
Article posted by Raja on 4/15/2008 | Views: 85981 | Category: JavaScript | Level: Intermediate red flag

Advertisements

Advertisements
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.

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


<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 !!
Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Raja Dutta

Experience:5 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, June 02, 2008
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

To check the IE 8.0 Version after install in server

This is HTML application which is mainly used in e-Commerce applications where the online shopping is done. All the registered users who logs in can access this appliction by selecting the products they wanted. The code might look clumsy but just copy paste and enjoy the article !!!

Here was the challenge for me, I had two dates like FromDate and ToDate. Lets say 01/01/2007 as FromDate and 01/01/2008 as ToDate. I need to traverse through all the dates in the given period. Initially, I was facing a lot of problem, however after many trial and error, I got it. Wish to share the code.

For many reasons sometime we don't want to allow user to use right click to copy paste or by using ctrl+C , ctrl+v to copy and paste in textbox on a aspx page in asp.net

Prototype is used to define classes or objects in JavaScript. Classes or objects are building blocks of Object Oriented Programming.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 7:45:16 AM