Get RadioButtonList's selected value in js

  • Thread starter Thread starter mg
  • Start date Start date
M

mg

After a selection is made in the following RadioButtonList

<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem Value="Y"></asp:ListItem>
<asp:ListItem Value="N"></asp:ListItem>
</asp:RadioButtonList>


the following script [in a client-side CustomValidator]
returns null, not "Y" or "N"

<script language="javascript">
q = document.getElementById
("RadioButtonList1").getAttribute("SelectedValue")
alert(q)
</script>

Can you tell me what I'm doing wrong?
 
As far as I know, you can't get the selected value of a radio button
directly. You need to iterate through each item in the group, until you
find the selected item, and then get its value. An example of how to do
this is below:

<script language="javascript">
function GetVal()
{
var a = null;
var f = document.forms[0];
var e = f.elements["RadioButtonList1"];

for (var i=0; i < e.length; i++)
{
if (e.checked)
{
a = e.value;
break;
}
}
return a;
}
</script>

Hope this helps,

Mun
 
Thanks!

-----Original Message-----
As far as I know, you can't get the selected value of a radio button
directly. You need to iterate through each item in the group, until you
find the selected item, and then get its value. An example of how to do
this is below:

<script language="javascript">
function GetVal()
{
var a = null;
var f = document.forms[0];
var e = f.elements["RadioButtonList1"];

for (var i=0; i < e.length; i++)
{
if (e.checked)
{
a = e.value;
break;
}
}
return a;
}
</script>

Hope this helps,

Mun



mg said:
After a selection is made in the following RadioButtonList

<asp:RadioButtonList id="RadioButtonList1" runat="server">
<asp:ListItem Value="Y"></asp:ListItem>
<asp:ListItem Value="N"></asp:ListItem>
</asp:RadioButtonList>


the following script [in a client-side CustomValidator]
returns null, not "Y" or "N"

<script language="javascript">
q = document.getElementById
("RadioButtonList1").getAttribute("SelectedValue")
alert(q)
</script>

Can you tell me what I'm doing wrong?



.
 
Back
Top