How to add a specific class to select2 drop element?
containerCssClass
works if you include select2.full.js
. Full version has this option
You can use dropdownCssClass
for adding class to the select2-drop
. I read whole plugin to understand what is going on. Finally, I found that option.
$(".jSelectbox").select2({
containerCssClass: "error",
dropdownCssClass: "test"
});
Update: If it didn't work, try this (way 2):
$select = $(".jSelectbox").select2({});
$select.data('select2').$container.addClass('error');
$select.data('select2').$dropdown.addClass('test');
$(".jSelectbox").select2({
containerCssClass: "error",
dropdownCssClass: "test"
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script>
<select class="jSelectbox">
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="CA">California</option>
<option value="CO">Colorado</option>
<option value="CT">Connecticut</option>
<option value="DE">Delaware</option>
<option value="FL">Florida</option>
<option value="GA">Georgia</option>
<option value="HI">Hawaii</option>
<option value="ID">Idaho</option>
<option value="IL">Illinois</option>
<option value="IN">Indiana</option>
<option value="IA">Iowa</option>
<option value="KS">Kansas</option>
<option value="KY">Kentucky</option>
<option value="LA">Louisiana</option>
<option value="ME">Maine</option>
<option value="MD">Maryland</option>
<option value="MA">Massachusetts</option>
<option value="MI">Michigan</option>
<option value="MN">Minnesota</option>
<option value="MS">Mississippi</option>
<option value="MO">Missouri</option>
<option value="MT">Montana</option>
<option value="NE">Nebraska</option>
<option value="NV">Nevada</option>
<option value="NH">New Hampshire</option>
<option value="NJ">New Jersey</option>
<option value="NM">New Mexico</option>
<option value="NY">New York</option>
<option value="NC" selected="">North Carolina</option>
<option value="ND">North Dakota</option>
<option value="OH">Ohio</option>
<option value="OK">Oklahoma</option>
<option value="OR">Oregon</option>
<option value="PA">Pennsylvania</option>
<option value="RI">Rhode Island</option>
<option value="SC">South Carolina</option>
<option value="SD">South Dakota</option>
<option value="TX">Texas</option>
<option value="TN">Tennessee</option>
<option value="UT">Utah</option>
<option value="VT">Vermont</option>
<option value="VA">Virginia</option>
<option value="WA">Washington</option>
<option value="WV">West Virginia</option>
<option value="WI">Wisconsin</option>
<option value="WY">Wyoming</option>
</select>
Jsfiddle
It's possible to make it in another way - via data-* of select2
- (if don't want to add select2.full.js
). Use like it:
var select2 = $(".element").select2({/* Select2 Opts */});
Ok. Now push var select2
to console ( for understanding) like:
console.log($(select2).data('select2'));
You will see something like this:
e {$element: init(1), id: "select2-....", options: e, listeners: Object, dataAdapter: d…}
$container: init(1)
$dropdown: init(1) <-- what you need!
$element: init(1)
$results: init(1)
....
Ok, now just add specified class(-es) to select2's dropdown
in the same way:
select2.data('select2').$dropdown.addClass("...");
Also, via $(select2).data('select2')
you have access to others select2 elements (container, etc.).