user455100
user455100

Reputation: 151

JQuery: how to check a radio button?

So if this qstring var is set then i need to check a certain radio button

ive tried the following :

$("#AcctRB").attr('checked', 'checked');
$('input[id=AcctRB]:eq(1)').attr('checked', 'checked');

which does not work. I might have a syntax error

Here is the rb :

<input type="radio" onclick="accountShow()" id="AcctRB" runat="server" name="GuestAndAccountRB" enableviewstate="true" />Account


 <% If Request.QueryString("login") = "guest" Then%> 
                 <script type= "text/javascript">
                     $(".new-accnt-ad").show("slow");
                     $(".accountPanel").hide("slow");
                     $(".guestPanel").show("slow");
                     $("#AcctRB").attr('checked', 'checked');
                 </script>
                 <%Else%>
              <script type= "text/javascript">
                  $(".new-accnt-ad").hide("slow");
                  $(".accountPanel").show("slow");
                  $(".guestPanel").hide("slow");
                  $('input[id=AcctRB]:eq(1)').attr('checked', 'checked');
              </script>

                 <% End If %>

This simply does not check the radio button in either case ive tried.

Upvotes: 0

Views: 685

Answers (4)

David Ipsen
David Ipsen

Reputation: 1533

I agree with Squeegy. I had the same problem the other day, and the issue was that even though the selector seemed to be correct, it was not retrieving any results.

I recommend getting a more advanced browser toolkit such as the FireBug plugin for Firefox or using Chrome's Developer tools. Try your jQuery selectors out in the Console Window they provide, and see if the results are what you expect. These tools also allow you to place breakpoints in your JavaScript code to see if the selector is being called by the browser.

Update: To test out jQuery selectors in Firebug, enable the Console tab, then simply type out the selector. The console will respond back with those elements that were selected by the jQuery call.

Upvotes: 0

Alec
Alec

Reputation: 9078

The JS might run before all the elements are created. Check out: $(document).ready():

$(document).ready(function() {
   // put all your jQuery goodness in here.
});

It's also not necessary to do this with JavaScript; simply add it to the HTML directly, depending on the QueryString("login").

Upvotes: 0

Alex Wayne
Alex Wayne

Reputation: 187262

I would bet that your selectors are not returning any elements, because that should work. Although it's probably cleaner to set checked to true in attr().

Proof this works: http://jsfiddle.net/A7Bke/

Upvotes: 1

Avien
Avien

Reputation: 1260

$('input[id=AcctRB]:eq(1)').attr('checked', true);

Upvotes: 1

Related Questions