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

            alert(radiobox[0].checked);

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

2
  • Basic debugging first. Any errors in the error console? Is the ID you are using actually output in the browser, and is there a radio button with that ID? Commented May 13, 2010 at 11:13
  • Try putting <%=rdoRiskAccepted.ClientID%> in an invisible text box (with style="display:none") and then access the id of the text box. I don't think javascript can access .net variables (allthought I do not know much about .net so I could be flagrantly wrong). Second thought, pass is as a variable in js function. Commented May 13, 2010 at 11:18

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I am using asp:radiobuttonlist, so I would have ID only...no name
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.
@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.
1

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%>

1 Comment

rdoRiskAccepted.ClientID will return the complete client side ID of the control, with page/content placeholder etc IDs in place.
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.