select2 - how can I init and default to placeholder when no value is selected?











up vote
1
down vote

favorite












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:



enter image description here



Then, I init select2:



$('#el').select2({
allowClear: true,
placeholder: '(select...)'
});


and combo turns into select2 control with "United States" being selected:



enter image description here



However, I would like it to look like this:



enter image description here



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>.










share|improve this question
























  • Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
    – smcd
    Apr 13 '16 at 18:41










  • The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
    – temuri
    Apr 13 '16 at 20:40








  • 2




    It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
    – smcd
    Apr 13 '16 at 21:25

















up vote
1
down vote

favorite












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:



enter image description here



Then, I init select2:



$('#el').select2({
allowClear: true,
placeholder: '(select...)'
});


and combo turns into select2 control with "United States" being selected:



enter image description here



However, I would like it to look like this:



enter image description here



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>.










share|improve this question
























  • Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
    – smcd
    Apr 13 '16 at 18:41










  • The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
    – temuri
    Apr 13 '16 at 20:40








  • 2




    It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
    – smcd
    Apr 13 '16 at 21:25















up vote
1
down vote

favorite









up vote
1
down vote

favorite











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:



enter image description here



Then, I init select2:



$('#el').select2({
allowClear: true,
placeholder: '(select...)'
});


and combo turns into select2 control with "United States" being selected:



enter image description here



However, I would like it to look like this:



enter image description here



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>.










share|improve this question















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:



enter image description here



Then, I init select2:



$('#el').select2({
allowClear: true,
placeholder: '(select...)'
});


and combo turns into select2 control with "United States" being selected:



enter image description here



However, I would like it to look like this:



enter image description here



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>.







javascript jquery-select2 jquery-select2-4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 16 hours ago









Nick

2,33312043




2,33312043










asked Apr 13 '16 at 18:27









temuri

1,34232347




1,34232347












  • Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
    – smcd
    Apr 13 '16 at 18:41










  • The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
    – temuri
    Apr 13 '16 at 20:40








  • 2




    It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
    – smcd
    Apr 13 '16 at 21:25




















  • Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
    – smcd
    Apr 13 '16 at 18:41










  • The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
    – temuri
    Apr 13 '16 at 20:40








  • 2




    It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
    – smcd
    Apr 13 '16 at 21:25


















Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
– smcd
Apr 13 '16 at 18:41




Possible duplicate of stackoverflow.com/questions/21413241/…. See answer
– smcd
Apr 13 '16 at 18:41












The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
– temuri
Apr 13 '16 at 20:40






The "accepted" answer does not work in the suggested solution. Firstly, it does not render the placeholder, secondly it also renders empty element as first option. Not helpful.
– temuri
Apr 13 '16 at 20:40






2




2




It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
– smcd
Apr 13 '16 at 21:25






It does work? jsfiddle.net/kqqeet2s/1 from the documentation select2.github.io/…
– smcd
Apr 13 '16 at 21:25



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36606622%2fselect2-how-can-i-init-and-default-to-placeholder-when-no-value-is-selected%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36606622%2fselect2-how-can-i-init-and-default-to-placeholder-when-no-value-is-selected%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft