This is very similar to this question on how to add class to select2 element, however the answers there appear to target earlier versions of the framework which has undergone some breaking changes in v4.0
According to this issue to add an additional custom class to select2-container, there were several undocumented properties you could pass to the select2 constructor, including: containerCss
, containerCssClass
, dropdownCss
, and dropdownCssClass
.
However in version 4 when I run the following code:
$('.select2').select2({
containerCss: "wrap"
});
I get the following error:
Uncaught Error: No select2/compat/containerCss
How can I pass a class to Select2 in v4.0?
Here's an example in StackSnippets:
$('.select2').select2({
containerCss: "wrap"
});
<link href="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/css/select2.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.js"></script>
<select class="select2 narrow wrap">
<option value="AL">Algebra</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
</select>