17

I need to disable options with value "- Sold Out -" in a list of dynamic drop down menus. How can I do this easily with jQuery? Below is the HTML

<select id="field_0_1" class="text_select" name="field_0_1" onChange="">
<option value="">- Preferred Time -</option>
<option value="- Sold Out -">- Sold Out -</option>
<option value="2:30 - 4:00pm">2:30 - 4:00pm</option>
</select>
<select id="field_0_2" class="text_select" name="field_0_2" onChange="">
<option value="">- Preferred Time -</option>
<option value="- Sold Out -">- Sold Out -</option>
<option value="2:30 - 4:00pm">2:30 - 4:00pm</option>
</select>
<select id="field_0_3" class="text_select" name="field_0_3" onChange="">
<option value="">- Preferred Time -</option>
<option value="- Sold Out -">- Sold Out -</option>
<option value="2:30 - 4:00pm">2:30 - 4:00pm</option>
</select>

6 Answers 6

49
$("select option[value*='Sold Out']").prop('disabled',true);
        ​

Demo

According to edit

$('#previous_select').on('change', function() {
   // after creating the option
   // try following
   $("select option[value*='Sold Out']").prop('disabled',true);
});
Sign up to request clarification or add additional context in comments.

4 Comments

LIfe is good bruv! :) wasn't well bruv, and was busy travelling from work.
Thanks guys, but my drop down is a dependant drop down and it populates <option value="- Sold Out -">- Sold Out -</option> when I select the previous drop down. So it doesnt get disabled with the script you provided. Whats the solution for that? How can I make it run the jquery once they change the drop down? Hope I make sense
Sorry, it didnt work either. Sorry. can you have a look at this link www.fitfixtraining.com.au/formtest/form.php Its a similar form as I have, in this case, I want to disable Alberta?
i changed .chagne to .click and it worked. But on IE, the drop down dissapears once clicked, and have to click it again
9

Working demo http://jsfiddle.net/BYkVW/ or http://jsfiddle.net/BYkVW/1/

Hope it helps the needs :)

code

$("#field_0_1 option[value='- Sold Out -']").attr('disabled','disabled');
        ​

or

$("#field_0_1 option[value='- Sold Out -']").prop('disabled','disabled');

working image

enter image description here

7 Comments

Thanks guys, but my drop down is a dependant drop down and it populates <option value="- Sold Out -">- Sold Out -</option> when I select the previous drop down. So it doesnt get disabled with the script you provided. Whats the solution for that? How can I make it run the jquery once they change the drop down?
Yo @UdaraUragoda can you flick this behaviour in jsfiddle? plz! :)
@UdaraUragoda cool so you second drop down disable is depended on the 1st drop down list right? so what is the condition you wnat option to be disabled for? Lemme know and I will flick you the solution. :) or help you out.
If the second option value has "Sold Out" somewhere within the value, disable it?
@UdaraUragoda yo brother - I am really finding it hard to understand what are your trying to say - I reckon think about the scenario and flick it across - i.e. you want option "Sold Out" disabled everywhere in the select drop down if it exist in multiple place? :) dont worry Given I understand I am happy to help you out!
|
3
function lockDownDropDownList(ddlName) {
    ddlName = "#" + ddlName;
    var chosenValue = $(ddlName).val();

    var downDownListItems = $(ddlName).children('option').map(function (i, e) {
        return e.value || e.innerText;
    }).get();

    downDownListItems.forEach(function (item) {
        if (item != chosenValue)
        {
            $("select option[value*='" + item + "']").prop('disabled', true);
        }
    });
}

Comments

1

Here i have done solution for above query. demo link as below:

Demo: http://codebins.com/bin/4ldqp92

HTML:

 <select id="field_0_1" class="text_select" name="field_0_1" onChange="">
  <option value="">
    - Preferred Time -
  </option>
  <option value="- Sold Out -">
    - Sold Out -
  </option>
  <option value="2:30 - 4:00pm">
    2:30 - 4:00pm
  </option>
</select>
<select id="field_0_2" class="text_select" name="field_0_2" onChange="">
  <option value="">
    - Preferred Time -
  </option>
  <option value="- Sold Out -">
    - Sold Out -
  </option>
  <option value="2:30 - 4:00pm">
    2:30 - 4:00pm
  </option>
</select>
<select id="field_0_3" class="text_select" name="field_0_3" onChange="">
  <option value="">
    - Preferred Time -
  </option>
  <option value="- Sold Out -">
    - Sold Out -
  </option>
  <option value="2:30 - 4:00pm">
    2:30 - 4:00pm
  </option>
</select>

JQuery:

$(function() {
    $("select").click(function() {
        $(this).find("option[value*='Sold Out']").prop("disabled", true);
    });
});

Demo: http://codebins.com/bin/4ldqp92

Comments

1

In case anyone wants to be able to disable a drop down list by text instead of value, here's what I did:

$("#DDL option").filter(function () {
    return $(this).text() === "Text 1" ||
           $(this).text() === "Text 2" ||
           $(this).text() === "Text 3";
}).prop("disabled", true);

Comments

0

Just use add class disabled to md-select-wrapper $("#selectFromMainId").addClass("disabled");

md-select-wrapper disabled disabled

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.