Search code examples
javascriptc#jqueryasp.netradiobuttonlist

Getting attribute for selected value from asp:RadioButtonList with jQuery


I'm maintaining some old c# code and there's an asp:RadioButtonList...

<asp:RadioButtonList AutoPostBack="false" TextAlign="Right" RepeatDirection="Horizontal" RepeatLayout="Flow" runat="server" ID="radIncidentType" />

... which is populated in the code behind like so:

    public static void PopulateRBL(string ListName, ref RadioButtonList ListToPopulate)
    {
        List<Lookup> radioList = GetLookup(ListName);  //returns
        foreach (Lookup entry in radioList)
        {
            ListItem item = new ListItem(" " + entry.Description + " ", entry.Code);
            item.Attributes.Add("ItemId", entry.Id.ToString());
            ListToPopulate.Items.Add(item);
        }
    }

So each item added has a description, a code, and an extra attribute ItemId.

There's then some validation performed on the onChange which has to interrogate the ItemId attribute. Currently is does it like this:

$('#ctl00_cphPage_radIncidentType input[type=radio]:checked').closest('span').attr('ItemId')

Which was working fine until I added a nested masterpage and to get it working I had to change the selector to:

$('#ctl00_ctl00_cphPage_nestedPage_radIncidentType input[type=radio]:checked').closest('span').attr('ItemId')

Obviously I'd like a neater selector, I've tried:

$('#<%= radIncidentType.ClientID %> input[type=radio]:checked').closest('span').attr('ItemId')

and ...

$("input[name='<%=radIncidentType.UniqueID%>']:radio:checked").closest('span').attr('ItemId')

...but neither work. Can anyone suggest a way of getting the value for that ItemId attribute?


Solution

  • I tried the following and it works as desired...

    $("[id$='_radIncidentType'] input[type=radio]:checked").closest('span').attr("ItemId")