Search code examples
c#-4.0radiobuttonlist

How to get the value of Radiobutton Listitem using Javascript


I want to get the value of the selected item of a radiobutton List using javascript. My code is:

 <asp:RadioButtonList runat="server" ID="Radio" RepeatColumns="3" CssClass="textfont">
                            <asp:ListItem Value="1" Selected="True">First</asp:ListItem>
                            <asp:ListItem Value="2">Second</asp:ListItem>
                            <asp:ListItem Value="3">Third</asp:ListItem>
                        </asp:RadioButtonList>

And this is my Javascript code:

<script type="text/javascript">
    function sendParameters() { 
        var Id = '<%=HiddenField1.Value%>';
        var ddl1 = document.getElementById("Radio").checked; 
</script>

How to proceed?


Solution

  • First of all please view the source of the resulting web page and post the resulting radio button html. This will make it easier to answer because then the question comes down to plain HTML and jQuery.

    The Reason is asp often changes the name of the ID, unless you add ClientIDMode="Static" to your control.

    Once that is done this should do it:


    var chosenValue = $('input:radio[id="Radio"]:checked').val();
    
    alert(chosenValue);