views:

54

answers:

3
var radiobox = document.getElementById('<%=rdoRiskAccepted.ClientID%>');

            alert(radiobox[0].checked);

I am getting undefined as a alert. what am I doing wrong.

+1  A: 

use alert(radiobox .checked);

I would also like to know why you are using clientid only you have to use <%=this.page.clientid + "_" + rdoRiskAccepted.ClientID%>

Ashutosh Singh
rdoRiskAccepted.ClientID will return the complete client side ID of the control, with page/content placeholder etc IDs in place.
Zhaph - Ben Duguid
+5  A: 

getElementById returns a single element because, even for radio groups, the id attribute must be unique. You should use the name attribute to specify a radio group and use getElementsByName instead. For instance:

<input type="radio" name="myRadio" checked><label>1</label>
<input type="radio" name="myRadio"><label>2</label>

JS

var radiobox = document.getElementsByName("myRadio");
alert(radiobox[0].checked);
Andy E
I am using asp:radiobuttonlist, so I would have ID only...no name
vaibhav
seriously? ASP doesn't give you access to the name? well, if so, just use the ID to get the first one, then access the .name property to get the name of the radio button set, then access by index.
scunliffe
@vaibhav: I don't work with ASP, but your ASP.net markup would still have to render into valid HTML code. According to some of the pages I've read, radio buttons are rendered with the `id` of the list as their `name`, so using `document.getElementsByName('<%=rdoRiskAccepted.ClientID%>');` should work.
Andy E
@Andy Thanks for the expert comment.
vaibhav
A: 

With jQuery...

<asp:radiobuttonlist ID="rdoRiskAccepted" runat="server">
    <asp:listitem Value="True" Selected="true">Yes</asp:listitem>
    <asp:listitem Value="False">No</asp:listitem>
</asp:radiobuttonlist>

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
    var riskAccepted=$('#<%=rdoRiskAccepted.ClientID%> input');
    alert(riskAccepted[0].checked); //true
});
//]]>
</script>
Matt