Cannot set value of select element in internet explorer 10 or 11

When ever I try to set the value of a select element like select.value='somevalue'; This no longer works in IE 10 or 11.

The JQuery version will work but not the raw JavaScript.

Does not work in IE 10 or 11

selFractInchOpts.value = '25';

Works in all browsers.

$(selFractInchOpts).val('25');

What is the correct way to set the value of a select element in 10 or 11 Internet Explorer?

--This works--

    var resetSelectValue = function (b) {

        var fract = (b.size.widthInches - ~~b.size.widthInches) + '',
                     width = (~~b.size.widthInches) + '',
                     o;

        $(selBayWidthOptions).val(width);
        $(selFractInchOpts).val(fract);

        selFractInchOpts.onchange = selectBayWidthOpts;
        selBayWidthOptions.onchange = selectBayWidthOpts;
    };

--This does not work--

            var resetSelectValue = function (b) {

                var fract = (b.size.widthInches - ~~b.size.widthInches) + '',
                             width = (~~b.size.widthInches) + '',
                             o;

                selBayWidthOptions.value = width;
                selFractInchOpts.value = fract;

                selFractInchOpts.onchange = selectBayWidthOpts;
                selBayWidthOptions.onchange = selectBayWidthOpts;
            };

Upvotes: 2

Views: 2554

Answers (1)

Charlie Affumigato
Charlie Affumigato

Reputation: 1027

What happens is that jQuery someSelect.val(x) is an entire function that iterates over all the options from the select, a quick and dirty option would be:

function setSelectValue(theSelect,value){
  Array.prototype.forEach.call(theSelect.children,function(el){if(el.value==value.toString()){el.selected=true}});
}

...your code where you need to set the element by its value
setSelectValue(theSelect,theValueToSelect);

Hope this helps.

Upvotes: 3

Related Questions