lou / multi-select

A user-friendlier drop-in replacement for the standard select with multiple attribute activated.
loudev.com
MIT License
1.9k stars 439 forks source link

opt.selected = true not reflected #324

Open stdedos opened 5 years ago

stdedos commented 5 years ago

I am using the example code http://loudev.com/#Searchable almost verbatim.

I have:

function pre_select_options(select_element, pre_select_options) {
    // // v2: awesome, but not working. multi-select error?
    // $(`${TC_SELECT_ELEMENT} option`).prop("selected", function () {
    //  return ~$.inArray(this.text, pre_select_options) ? 'selected' : false;
    // });

    $(`${TC_SELECT_ELEMENT} option`).prop("selected", function () {
        return ~$.inArray(this.text, pre_select_options) ? 'selected' : false;
    });
}
window.onload = function() {
    pre_select_options($(TC_SELECT_ELEMENT)[0], window.location.hash.slice(1).split(','));

    //example-init-code
}

and, even though the element on devtools says $0.selected = true, it is not shown on the second list. Only rendering the page with selected="selected" works.

I might be missing the obvious (as I have very limited js experience), but kindly help.