Get RadioButtonList's selected value in js


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?
 
Ad

Advertisements

M

Munsifali Rashid

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
 
Ad

Advertisements

M

mg

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?



.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top