jquery select item in radiobuttonlist via client-side function

mcliedtk picture mcliedtk · Mar 18, 2010 · Viewed 10k times · Source

I have the following ASP.NET RadioButtonList:

<asp:RadioButtonList ID="rbl" runat="server">
    <asp:ListItem Text="Type1" Value="1" />
    <asp:ListItem Text="Type2" Value="2" />
</asp:RadioButtonList>

I would like to select an item in the list programmatically via a client-side jquery function like this (simplified version):

function BindWidget(widget) {
    // Type property of Widget is an int.
    $("#<%=rbl.ClientID%>").selectItemByValue(widget.Type);
}

Ideally, there is some function - in the above code I have proposed selectItemByValue - that selects an item in a RadioButtonList by a given value. Does jquery have a similar function built-in? If not, how should I go about implementing the desired functionality?

Answer

SoftwareGeek picture SoftwareGeek · Mar 19, 2010

try this.

$('#<%=rbl.ClientID %>').find("input[value=" + widget.Type + "]").attr("checked", "checked");