atamata atamata - 1 year ago 104
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>

...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');


$("#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 Source

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);


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

Working Demo

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download