I'm trying to use the Select2 jquery plugin to make the optgroups in my select drop down menu collapsible/expandable.
I found what looks to be a good and straightforward solution (https://github.com/select2/select2/issues/730) but it's not working for me and I think it's because it's from an older version of select2.
I've figured out how to update the css from
.select2-result-sub > li.select2-result {
display: none;
}
to
#select-container .select2-results__options--nested > li.select2-results__option {
display: none;
}
(where i've also put my select object in a container per How to override .select2-results .select2-highlighted color)
But I'm at a loss for how to update the required javascript:
$('.select2-results').on('click', 'li', function(){
$(this).find('li').show();
});
It seems to me like I would just need to replace select2-results
with select2-results__options
or select2-results__option
, but I've tried a number of things, with and without using the container and I just can't get it to work. I don't have much experience in javascript/jquery so I don't really understand what I'm doing wrong with trying to select the optgroup element.
Also, here's my html:
<div id="select-container">
<select id="select-test" multiple="multiple" style="width:300px">
<optgroup label="Group 1">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</optgroup>
<optgroup label="Group 2">
<option value="4">Option 4</option>
<option value="5">Option 5</option>
<option value="6">Option 6</option>
</optgroup>
</select>
</div>
Any help is appreciated, thanks!!