My goal is to only allow 1 option in an optgroup to be selected from the select2 plugin.
Please see this code example: https://jsfiddle.net/xpvt214o/554423/
The problem I’m having is with this code:
$portSelect.on('select2:unselect', function (evt) {
$portSelect.find('optgroup').each(function (gindex) {
var $group = $(this);
var selected = 0;
$group.children().each(function (cindex) {
if ($(this).prop('selected') === true) {
selected++;
}
});
if (selected != 0) {
$group.children().removeAttr('disabled');
}
});
$portSelect.select2('destroy').select2(select2options);
});
Specifically the $portSelect.select2('destroy').select2(select2options);
line. Commenting this line prevents the error from happening, but does not refresh the select2 options…
When you select an option, then deselect that option, you get the following console error:
select2.min.js:1 Uncaught TypeError: Cannot read property 'query' of null
I posted this on StackOverflow but had no answers.