8

have 2 RadioButtonLists:

Are you a minor?:  oYes  oNo
Do you wish a joint account?:  oYes  oNo

If the answer to the 1st question is yes, I want to detect the response to the first question and set the answer to the 2nd question to yes using a jQuery function. Thanks in advance.

<asp:Label ID="lblMinfor" CssClass="boldIt" runat="server" Text="Is this account for a minor" Style="float: left; width: 200px;"></asp:Label><br />
<asp:RadioButtonList ID="rdlMinor" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow" ()>
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
<asp:Label ID="lblJoint" CssClass="boldIt" runat="server" Text="Is this for a joint account?" Style="float: left; width: 200px;"></asp:Label><br />
<asp:RadioButtonList ID="rdlJoint" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow">
    <asp:ListItem>Yes</asp:ListItem>
    <asp:ListItem Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
Bengi Besçeli
  • 3,638
  • 12
  • 53
  • 87
Susan
  • 1,822
  • 8
  • 47
  • 69

2 Answers2

11
$('#<%=rdlMinor.ClientID %>').change(function() {
    if($('#<%=rdlMinor.ClientID %> input:checked').val() == 'Yes') 
    {
        $('#<%=rdlJoint.ClientID %>').find("input[value='Yes']").attr("checked", "checked");
    }
});
kevev22
  • 3,737
  • 21
  • 32
  • When ASP.Net is rendered as HTML, the ID is changed and no longer 'rdlMinor;' it is created on the fly. That ID is not available when the jQuery script is loaded in memory so it is not accessible in the jQuery. – Susan Feb 24 '12 at 13:43
  • You can use ClientIDMode="Static" on your control to keep the ID defined in your markup. – Andreas Feb 24 '12 at 14:22
4

Try this code:

<script type="text/javascript">

$(document).ready(function() {

    $("#<%=rdlMinor.ClientID%>").change(function()
    {
        var rbvalue = $("input[@name=<%=rdlMinor.ClientID%>]:radio:checked").val();

        if(rbvalue == "Yes")
        {
            $('#<%=rdlJoint.ClientID %>').find("input[value='Yes']").attr("checked", "checked");
        }
    });

});

</script>

More on these subjects:

JQuery Accessing the Client Generated ID of ASP.NET Controls

How to Set Get RadioButtonList Selected Value using jQuery

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • When ASP.Net is rendered as HTML, the ID is changed and no longer 'rdlMinor;' it is created on the fly. That ID is not available when the jQuery script is loaded in memory so it is not accessible in the jQuery. – Susan Feb 24 '12 at 13:43
  • That's why we're using `$("#<%=rdlMinor.ClientID%>")` and not only `$(#rdlMinor")`. This first construct will place in the jQuery code the exact ID that ASP.NET is using for that control. It's indeed dynamic. That's why we need to use `$("#<%=rdlMinor.ClientID%>")`. By the way, you should put your jQuery code after the page has finished loading in the jQuery ready event. I'll update the code. – Leniel Maccaferri Feb 24 '12 at 14:13
  • I just corrected a typo in my previous code where I had `<%=dlMinor.ClientID%>` instead of `<%=rdlMinor.ClientID%>`. See if the above corrected code works now and get back... – Leniel Maccaferri Feb 24 '12 at 14:27