bluzky / nice-select2

A lightweight vanilla javascript library that replaces native select elements with customizable dropdowns
https://bluzky.github.io/nice-select2/
MIT License
361 stars 60 forks source link

updateSelectValue multiple issue #61

Open afonja14755 opened 1 year ago

afonja14755 commented 1 year ago

When user check and uncheck item, in native select, option doesn't remove attribute selected=true.

You can fix it by:

var select = this.el;
select.querySelectorAll("option").forEach(function (item) {
  item.removeAttribute("selected");
});
this.selectedOptions.forEach(function(item) {
  var el = select.querySelector(`option[value="${item.data.value}"]`);
  if (el){
    //Change it, cause it works more correctly.
    el.setAttribute("selected", "selected");
    el.selected = true;
  }
});

Or some better solution with indexOf, filter.

gpham99 commented 5 months ago

+1 on this. This would be so nice

afonja14755 commented 5 months ago

+1 on this. This would be so nice

If you are looking for a new and supported library for selectors in native JavaScript, I can recommend you my own, which I am following.