Search code examples
dropdownhtml-selectdisable

How to disable dropdown option values based on another dropdown


Searching over the google and stack overflow, could not find a solution to this.

I have two dropdown in MVC core view

dropdown a looks like following:

<option>A</option>
<option>B</option>

and second dropdown looks like following:

<option>A1</option>
<option>A2</option>
<option>A3</option>
<option>B1</option>
<option>B2</option>

I need to disable A1,A2,and A3 in second dropdown when B is selected in the first dropdown and disable B1, and B2 when A is selected in the first dropdown.

I tried the following but it is not working:

    $('#FirstDropDown').change(function () {
        var data = $(this).val();
        if (data == "A") {
            $("#SecondDropDown option[value='A1']").attr('disabled', 'disabled');
            $("#SecondDropDown option[value='A2']").attr('disabled', 'disabled');
            $("#SecondDropDown option[value='A3']").attr('disabled', 'disabled');
        }
    });

Solution

  • When you are trying to access a value in $("#SecondDropDown option[value='A1']"), there shouldn't be single quotes surrounding the value, and it is intended for the HTML value attribute instead, so it would be more appropriate like this:

    $('#FirstDropDown').change(function () {
        var data = $(this).val();
        if (data == "A") {
            $("#SecondDropDown option[value=B1]").attr('disabled', 'disabled');
            $("#SecondDropDown option[value=B2]").attr('disabled', 'disabled');
            $("#SecondDropDown option[value=A1]").prop('disabled', false);
            $("#SecondDropDown option[value=A2]").prop('disabled', false);
            $("#SecondDropDown option[value=A3]").prop('disabled', false);
        }
        if (data == "B") {
            $("#SecondDropDown option[value=A1]").attr('disabled', 'disabled');
            $("#SecondDropDown option[value=A2]").attr('disabled', 'disabled');
            $("#SecondDropDown option[value=A3]").attr('disabled', 'disabled');
            $("#SecondDropDown option[value=B1]").prop('disabled', false);
            $("#SecondDropDown option[value=B2]").prop('disabled', false);
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select id="FirstDropDown">
        <option selected="selected">A</option>
        <option>B</option>
    </select>
    
    <select id="SecondDropDown">
        <option value="A1">A1</option>
        <option value="A2">A2</option>
        <option value="A3">A3</option>
        <option value="B1" disabled="disabled">B1</option>
        <option value="B2" disabled="disabled">B2</option>
    </select>

    Notice you have to un-disable/re-enable the opposite letter when the user switches, since that may happen. I used the prop() method for that. Also notice you need to have B1 and B2 disabled in the beginning, since A is the first value of the drop-down list, so it is the default selected value to begin with.