atamata atamata - 3 months ago 19
ASP.NET (C#) Question

jQuery disable dropdownlist item by text

I have a asp:net dropdownlist which has extra items appended server side:

<asp:DropDownList ID="ddlResult" runat="server">
<asp:ListItem Value="" Text="-- Please Select --"></asp:ListItem>
</asp:DropDownList>


...and basically I would like to disable/enable an entry by the display text in jQuery. I've tried the numerous examples on here but none of them seem to work for me. eg.

$("#ddlResult option[text='Positive']").attr('disabled','disabled');


..or...

$("#ddlResult option[text='Positive']").hide();


...or ...

$("#<% =ddlResult.ClientID %> > option[text='Positive']").attr("disabled", "disabled")


...amongst others.

Can anyone advise where I am going wrong?

Thanks in advance.

Answer

May be something like this. You can use the :contains selector to find the text and disable the option.

$('#ddlResult option:contains("Positive")').prop("disabled", true);

or

$('#ddlResult option:contains("Positive")').attr("disabled", "disabled");

Working Demo