I am trying to get select2
to display placeholder when HTML <select>
does not have an option selected.
My HTML select - no options have "selected" attribute and there's no "empty" option either:
<select id="el" style="width:300px">
<option value="1">United States</option>
<option value="2">Canada</option>
</select>
On page load I do this:
$('#el').val(null);
As a result, my combo looks like this - you can see that no option is selected:
Then, I init select2
:
$('#el').select2({
allowClear: true,
placeholder: '(select...)'
});
and combo turns into select2
control with "United States" being selected:
However, I would like it to look like this:
I.e. when my combo's value is NULL (or selectedIndex is -1), I would like select2
to display the placeholder (select...) as opposed to the first <option>
.