How can get radio button list checked value in JavaScript

Posted by Sekar.C under JavaScript on 8/7/2012 | Points: 10 | Views : 23879 | Status : [Member] | Replies : 2
Hi,
How can get radio button list checked value in JavaScript
I have two radio lists (male, female) if I check male radio list print 'M' in textbox
How can I do

Regards
Sekar.c



Responses

Posted by: Ogipansrk on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
Ue the below code ,


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">

function call() {
var selectedvalue;

if (document.getElementsByName('<%= RadioButtonList1.ClientID %>')) {


var radbtnList = document.getElementsByName('<%= RadioButtonList1.ClientID %>');
for (var i = 0; i <= radbtnList.length; i++) {
if (radbtnList[i].checked) {
selectedvalue = radbtnList[i].value;
alert(selectedvalue);

}
}
}

}

</script>

</head>
<body>
<form id="form1" runat="server">
<div>

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="1">Apple</asp:ListItem>
<asp:ListItem Value="2">Mango</asp:ListItem>
<asp:ListItem Value="3">Grapes</asp:ListItem>
</asp:RadioButtonList>
<asp:Button ID="Button1" runat="server" Text="Get Radio Button Value" OnClientClick="call()" />

</div>
</form>
</body>
</html>


Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 8/7/2012 [Member] [MVP] Gold | Points: 25

Up
0
Down
    <script language="javascript" type="text/javascript">

function getCheckedRadio() {
var radioButtons = document.getElementsByName("RadioButtonList1");
for (var x = 0; x < radioButtons.length; x++) {
if (radioButtons[x].checked) {
document.getElementById('TextBox1').value = radioButtons[x].value;
}
}
}
</script>


    

<asp:RadioButtonList ID="RadioButtonList1" runat="server" onClick="getCheckedRadio()">
<asp:ListItem Value="M">Male</asp:ListItem>
<asp:ListItem Value="F">Female</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>


Sekar.C, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response