Disabling an option after opening the dropdown


#1

Disabling an option like this works if the dropdown has not yet been opened:

example1.find('option').eq(1).attr('disabled', 'disabled');
example1.trigger('change.select2');

However it does not work if the dropdown has already been opened. If the dropdown has been opened, the only way I can disable the option is by replacing the option completely with a new identical option that has the disabled attribute set, like this:

var newOption = example1.find('option').eq(1).clone();
newOption.attr('disabled', 'disabled');
example2.find('option').eq(1).replaceWith(newOption);

Here’s a fiddle showing 3 cases:

  1. opening the dropdown, changing the disabled attribute, re-opening (doesn’t work)
  2. opening the dropdown, replacing the disabled option, re-opening (works)
  3. changing the disabled attribute, opening (works)

https://jsfiddle.net/7084syg2/14/

It seems to me that #1 should work whether or not the dropdown has been previously opened. Is that correct?

Thanks!


#2

Shoot, my fiddle used select2 4.0.2. It works with 4.0.12. Nevermind.