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:
- opening the dropdown, changing the disabled attribute, re-opening (doesn’t work)
- opening the dropdown, replacing the disabled option, re-opening (works)
- 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!