skip to Main Content

I have a signup.aspx page with a radio button list

            <asp:radiobuttonlist id="kita1" runat="server">
        <asp:listitem id="option1" runat="server" value="first" />
        <asp:listitem id="option2" runat="server" value="second" />
        <asp:listitem id="Listitem1" runat="server" value="third" />
</asp:radiobuttonlist>

and I need to get the value of the selected item and save it to a variable

how can I do that with javascript ?

2

Answers


  1. <form runat="server">
    <asp:RadioButtonList ID="RadioButtonList1" runat="server" 
              RepeatDirection="Horizontal" RepeatLayout="flow" >
             <asp:ListItem Selected="True" Text ="Yes" Value="0"></asp:ListItem>
             <asp:ListItem  Text ="No" Value="1"></asp:ListItem>
          </asp:RadioButtonList>    </form>
    <p id="button" onclick="getvalue()">click me</p>
    
    <script type="text/javascript">
    function getvalue(){
     alert($('#<%=RadioButtonList1.ClientID %> input[type=radio]:checked').val());
    }
    </script>
    
    Login or Signup to reply.
  2. try this way

     var list = document.getElementById("radios"); //Client ID of the radiolist
     var inputs = list.getElementsByTagName("input");
     var selected;
     for (var i = 0; i < inputs.length; i++) {
          if (inputs[i].checked) {
              selected = inputs[i];
              break;
           }
      }
      if (selected) {
           alert(selected.value);// use value
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search