Vaibhav Jain
Vaibhav Jain

Reputation: 34447

Need help in javascript code

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

            alert(radiobox[0].checked);

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

Upvotes: 0

Views: 100

Answers (3)

Matt
Matt

Reputation: 904

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>

Upvotes: 0

Andy E
Andy E

Reputation: 344803

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);

Upvotes: 5

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

Upvotes: 1

Related Questions