8

I need to do something like this:

<asp:RadioButton ID="rbDate" runat="server" Text="Date" GroupName="grpPrimary" />

and be able to check the value of the radio button's checked value in jQuery, but my attempts like these don't return true/false.

if ($('[name=rbDate]').attr("Checked"))

if ($('[name=rbDate]').attr("Checked").val())

if ($('[name=rbDate]:checked').val())

A little help?

rick schott
  • 21,012
  • 5
  • 52
  • 81
Dan Bailiff
  • 1,513
  • 5
  • 23
  • 38

4 Answers4

18

This is probably the easiest way to do it. The *= searches the whole id attribute for rbDate which takes care of the whole ASP.NET id mangling.

$('input[id*=rbDate]').is(":checked");
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
5

While ChaosPandion's answer will work it would be faster to wrap your RadioButtonList in a div like this:

<div id="dateWrapper">
    <asp:RadioButton 
        ID="rbDate" 
        runat="server" 
        Text="Date" 
        GroupName="grpPrimary" />
</div>

Then your jQuery code can be this simple:

var selected = $("#dateWrapper input:radio:checked");
Community
  • 1
  • 1
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
3

INamingContainer adds a bunch of stuff to the beginning of the id of the actual html.

$('input[id$=rbDate]').attr('checked')

using the [id$=rbDate] bit on the selector tells jQuery that you want the input with an id that ends in rbDate

Now if you had a that you wanted to get the selected value of the entire list you might do something like

$('input[name$=rbDate]:checked').val()

which, were one of the items selected would return the value of the selected , or , in that radio button list.

Aaron
  • 802
  • 7
  • 17
0

Here is my JQuery solution that uses the asp.net ID:

var rbSelected =  $("#<%= rbDate.ClientID %>").is(":checked"); 
Enkode
  • 4,515
  • 4
  • 35
  • 50