Select2 supports displaying a placeholder by default using the placeholder
option. This can be either a data object matching the placeholder option, or a string to display as the placeholder if you are using a blank placeholder option.
This usually means that you do not have a blank <option></option>
as the first option in your <select>
.
Note that this does not apply to multiple selects, as the browser does not select the first option by default when multiple selections can be made.
Yes, Select2 supports placeholders for all configurations. You will still need to add in the placeholder option if you are using a single select.
The placeholder
option allows you to pass in a data object instead of just a string if you need more flexibility. The id
of the data object should match the value
of the placeholder option.
When using Select2 when only a single selection can be made, the placeholder option will be passed through the standard templating methods, including the templateSelection
option, so you are able to change how it is displayed.
When multiple selections are allowed, the placeholder will be displayed using the placeholder
attribute on the search box. You can cusotmize the display of this placholder using CSS, as explained in the following Stack Overflow answer: Change an input's HTML5 placeholder color with CSS
Select2 uses the native placeholder
attribute on input boxes for the multiple select, and that attribute is not supported in older versions of Internet Explorer. You need to include Placeholders.js on your page, or use the full build, in order to add placeholder
attribute support to input boxes.