7 February 2014

How to get selected value from asp.net RadioButtonList using JavaScript?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    <script language="javascript" type="text/javascript">
        function GetSelectedValue() {
            var list = document.getElementById("<% =rdbtnLst.ClientID %>"); //Cleint ID of RadioButtonList
            var rdbtnLstValues = list.getElementsByTagName("input");
            var Checkdvalue;
            for (var i = 0; i < rdbtnLstValues.length; i++) {
                if (rdbtnLstValues.checked) {
                    Checkdvalue = rdbtnLstValues;
                    break;
                }
            }
            if (Checkdvalue) {
                alert(Checkdvalue.value);
            }
        }
    </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div>
        <asp:RadioButtonList ID="rdbtnLst" AutoPostBack="true" runat="server" RepeatDirection="Horizontal">
            <asp:ListItem Selected="True">One</asp:ListItem>
            <asp:ListItem>Two</asp:ListItem>
            <asp:ListItem>Three</asp:ListItem>
            <asp:ListItem>Four</asp:ListItem>
        </asp:RadioButtonList>
        <asp:Button ID="Button1" runat="server" OnClientClick="GetSelectedValue()" Text="Button" />
    </div>
</asp:Content>


No comments:

Post a Comment