Ronald McDonald
Ronald McDonald

Reputation: 2911

respond to asp:RadioButtonList changes with jquery

I have the code below with sets the focus on a textbox when the page loads and when the user selects a radio button.

How can I change this so that if they choose the last radio option "Mail" the focus is not set to txtPayerName but instead to the btnSubmit control?

 $(function () {
                    SetDefaultPaymentType();
                    $("#txtPayerName").focus();


                $("#rdLstPaymentOptions").change(function () {
                    $("#txtPayerName").focus();
                });

            });


<asp:RadioButtonList ID="rdLstPaymentOptions" runat="server" RepeatColumns="3" RepeatDirection="vertical"
                    RepeatLayout="Flow" OnPreRender="rdBtnLst_PreRender" TabIndex="1" 
                    ClientIDMode="Static">
                    <asp:ListItem Text="Credit Card&nbsp;&nbsp;&nbsp;" Value="CreditCard" ></asp:ListItem>
                    <asp:ListItem Text="Electronics Fund Transfer&nbsp;&nbsp;&nbsp;" Value="EFT"></asp:ListItem>
                    <asp:ListItem Text="Check By Mail&nbsp;&nbsp;&nbsp;" Value="Mail"></asp:ListItem>
                </asp:RadioButtonList>

Upvotes: 0

Views: 570

Answers (2)

ryan1234
ryan1234

Reputation: 7275

What if you set the change event on each radio button individually?

Something like:

$("#rdLstPaymentOptions").each(function (index) {  
   if (index == $("#rdLstPaymentOptions").length - 1) {
      // insert code to set change event for last radio button
   }
   else {
      ($this).change = function () {
         $("#txtPayerName").focus(); 
      }
   }
}); 

Upvotes: 1

Diodeus - James MacFarlane
Diodeus - James MacFarlane

Reputation: 114417

Give the radio button and the submit button a unique CSS class, then use:

$('.radioClass').click(function() {
    $('.submitClass').focus()
})

Upvotes: 0

Related Questions