Search code examples
javascriptc#asp.netasp.net-mvcenumdropdownlistfor

update value as selected value in EnumDropDownList


I created the DropDownList and EnumDropDownList.

Every option in DropDownList has an option with the same name on EnumDropDownList.

I take an option the user picked from DropDownList and I want to update EnumDropDownList that this specific option was selected by the user.

My DropDownList has an id (id="ImageTypeDropDown")
My EnumDropDownList has an id ("id="FileTypeDropDown")

How to fix the line:

document.getElementById('FileTypeDropDown2').options.find(wantedOption).Selected=true; 



    <script>
      function adjustFitted() {            
            var e = document.getElementById('ImageTypeDropDown');
            var wantedOption = e.options[e.selectedIndex].text;   // the option user chose - works well:)                         

            document.getElementById('FileTypeDropDown2').options.find(wantedOption).Selected=true;           

            return false;
        }
    </script>

Solution

  • You are very close.

    function adjustFitted() {
            var targetVal = document.getElementById('ImageTypeDropDown').value;
    
    
            var targetDropDown = document.getElementById('FileTypeDropDown2');
    
            for (var i = 0; i < targetDropDown.options.length; i++) {
                if (targetDropDown.options[i].value == targetVal) {
                    targetDropDown.options[i].selected = true;
                }
            }
    
    
            return false;
        }