8

I want to disable the dropdown list item using jquery. The index value is -1 and the value is ------- Internal -------. I tried below syntaxes one by one(The solutions were the answers for similar questions). None of them are working. Iam using IE8.

var value="------- Internal -------";
$("[id*='ChildOrganizationDropDownList'] option[value=" + value + "]").prop('disabled','disabled'); 
$("#ChildOrganizationDropDownList option[value=" + value + "]").prop('disabled','disabled');    
$("[id*='ChildOrganizationDropDownList']").option('------- Internal -------').prop('disabled',true);    
$("id*='ChildOrganizationDropDownList' option[value='------- Internal -------']").prop('disabled','disabled');    
$("[id*='ChildOrganizationDropDownList']").option("[value*='------- Internal -------']").prop('disabled', true);
$("[id*='ChildOrganizationDropDownList']").attr("disabled",$(_this.CustomerNameDropDownList).find("option[value='------- Internal -------']"));

Designer : Aspx

<div class="selectionControls">
  <asp:CheckBox ID="chkSubcontracting" runat="server" Text="Subcontracting" Enabled="true" />
  <asp:HiddenField ID="SubcontractingHiddenField" runat="server" />
  <div class="subSelectionControl" id="AllowSubcotractingSelection" style="display: none;">
    <div class="subContractingControls">
      Parent BU:
      <span>
        <div class="subContractingControls">
          Supplier <em class="mandatoryIndicator">*</em>:
          <span>
            <asp:DropDownList ID="ChildOrganizationDropDownList" runat="server" Width="200px" />
            <asp:HiddenField ID="IxChildOrganizationHiddenField" runat="server" />
          </span>
        </div>
    </div>
  </div>

Aspx.cs

public Dictionary<int, string> ChildOrganizations
        {
            set
            {
                var result = value;
                result.Add(0, "Select Supplier");
                ChildOrganizationDropDownList.DataSource = result;
                ChildOrganizationDropDownList.DataTextField = "value";
                ChildOrganizationDropDownList.DataValueField = "key";
                ChildOrganizationDropDownList.DataBind();
                ChildOrganizationDropDownList.SelectedValue = "0";


            }
        }
5
  • 1
    Can you show the html Commented Apr 10, 2015 at 11:13
  • @DarrenSweeney the designer code is added Commented Apr 14, 2015 at 6:19
  • The problem seems be in $("#ChildOrganizationDropDownList option") part. And has to be changed to some other syntax. Commented Apr 14, 2015 at 6:37
  • jsfiddle.net/dZqEu/3 Commented Apr 21, 2015 at 6:19
  • 1
    after applying my correct indentation edit you will see that 1 span and 2 divs aren't closed. This could prevent javascript from running correctly... just a guess Commented Apr 21, 2015 at 17:52

8 Answers 8

3

I used each and it worked for me: https://jsfiddle.net/pz9curkb/1/

<select id="ChildOrganizationDropDownList">
    <option value="Default">Default</option>
    <option value="A">A</option>
    <option value="B">------- Internal -------</option>
    <option value="E">E</option>
</select>


 $("#ChildOrganizationDropDownList option").each(function(){   
     if($(this).text() === "------- Internal -------"){
        this.disabled  = true;
     }
});

Please note in this solution I used this instead of $(this), which means I used disabled option provided by javascript

Sign up to request clarification or add additional context in comments.

1 Comment

@ManojNayak do you have a master page?
3
+50

First, ASP.NET might change the rendered client ID on your ChildOrganizationDropDownList control. In order to make this managable, you should set ClientIDMode to Static:

<asp:DropDownList ID="ChildOrganizationDropDownList" runat="server" Width="200px" ClientIDMode="Static" />

(See MSDN Library.) Now, you'll know that you can find this control using jQuery selector $("#ChildOrganizationDropDownList").

Secondly, I'm not sure that your JavaScript waits for the DOM to finish loading. If it fires too early, it will not find anything (since it's not loaded yet). Use $(document).ready(handler), or the shorthand $(handler).

Now, a JavaScript that disables all options with value "-1" in ChildOrganizationDropDownList could look like this:

$(function() { // Wait for the DOM to load
  $("#ChildOrganizationDropDownList option[value='-1']").attr("disabled", "disabled");
});

1 Comment

It is probably worth noting that ClientIDMode="Static" is "new" with .NET 4. If the OP is using a earlier version they will need to get the ClientID from the control into the JavaScript.
2

Try this:-

make sure you put the code in document.ready as below

$(document).ready(function(){
    $("select[ID$='ChildOrganizationDropDownList']").find("option").each(function(){   
         if($(this).text() === "------- Internal -------"){
            this.disabled  = true;
         }
    });    
});

Comments

1

Although I would advise you to rather provide support for 2 latest IE version rather than supporting IE8.

I tested the following code and it works fine.

    $(document).ready(function()
    {
        var valinp="------- Internal -------";
        $("#ChildOrganizationDropDownList option[value='" 
            +  valinp + "']").attr("disabled","");

    })

As you are supporting IE 8 you should include a jquery version which is < jQuery 2.x https://jquery.com/browser-support/

Comments

1

If you don't want the user to select that specific option why not hide it instead of disabling it?

$("#ChildOrganizationDropDownList option:contains('Internal')").hide();

Or even better remove it onclick of the object,

$(document).delegate("#ChildOrganizationDropDownList","click",function(){
    $(this).find("option:contains('Internal')").detach();
});

So they can't select it.

1 Comment

I am loading two set of values to the dropdown and i want to differentate between those
1

The following worked for me

$("#sel option[value=---Internal---]").prop('disabled','disabled');

Working JS Fiddle http://jsfiddle.net/jt47kj5n/1/

You can also try by wrapping the value with single quotes.

$("#sel option[value='---Internal---']").prop('disabled','disabled');

Comments

1

If the value -1 is never selectable accross your entire site then you can just do it very easily like this:

$('option[value=-1]').prop('disabled', true);

Otherwise you can do this:

  1. Wrap your dropdown in div tags with an id (or you can also define a unique class and use it for all dropdowns with those properties):

    <div id="my-dropwdown">
        <asp:DropDownList ... />
    </div>
    
  2. Select all the options inside the div with value="-1" and disable them:

    $('option[value=-1]', $('#my-dropdown')).prop('disabled', true);
    

    or

    $('option[value=-1]', $('#my-dropdown')).attr('disabled', 'disabled');
    

    and if you want to select over the class:

    $('option[value=-1]', $('.subContractingControls')).prop('disabled', true);
    

Comments

1

Actualy in ASP.net Webform architecture is also matter where you write your jQuery / javascript.

if you are adding script in your aspx page where control is place. you can use '<%= Control.ClientID %>' to get current if of control. If you are placing your code in aspx page but in header and your control is in your child page you can use

<%= Page.Master.FindControl("ContentPlaceHolder1").FindControl("Button1").ClientID %>'

  var currentControlID='<%= Control.ClientID %>';
    $("#"+currentControlID+" option").each(function(){   
     if($(this).text() === "------- Internal -------"){
        this.disabled  = true;
         }
    });

If you have separate js file then you have to set ClientIDMode="Static" for control and assign you id(but make sure this id must not be used for any other control and it must be unique in page.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.