Select2 doesn't show selected value
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Select2 loads all items from my list successful, the issue I found when try to select a specific value when page loads. Example:
:: put select2 in a specific html element, no value is selected even all items are loaded.
$('#my_id').select2();
:: When the page is loaded I'm trying to show a specific item selected, but doesn't work as expected, because even selected, the select2 doesn't show it.
$('#my_id').val('3'); //select the right option, but doesn't render it on page loads.
How to make a selected option to pop up when pages loads?
Thanks in advance.
UPDATED
:: How I load all select2 items (sorry, its jade, not pure HTML):
label(for='category') Category
span.required *
select(id='category', style='width:230px', name='category')
option(value='') - Select -
each cat in categories
option(value='#{cat.id}') #{cat.description}
P.S.: All items from my list are loaded.
:: How I initialize the select2:
Just put the following line code on my javascript and it does successful:
$('#category').select2();
:: How I'm trying to select a specific value:
First attempt:
$('#category').select2(
{
initSelection: function(element, callback) {
callback($('#field-category').val());
}
}
);
Second attempt:
$('#category').val($('#field-category').val());
P.S.: #field-category
has a value its a hidden input field and works OK.
Thanks, guys!
jquery-select2
add a comment |
Select2 loads all items from my list successful, the issue I found when try to select a specific value when page loads. Example:
:: put select2 in a specific html element, no value is selected even all items are loaded.
$('#my_id').select2();
:: When the page is loaded I'm trying to show a specific item selected, but doesn't work as expected, because even selected, the select2 doesn't show it.
$('#my_id').val('3'); //select the right option, but doesn't render it on page loads.
How to make a selected option to pop up when pages loads?
Thanks in advance.
UPDATED
:: How I load all select2 items (sorry, its jade, not pure HTML):
label(for='category') Category
span.required *
select(id='category', style='width:230px', name='category')
option(value='') - Select -
each cat in categories
option(value='#{cat.id}') #{cat.description}
P.S.: All items from my list are loaded.
:: How I initialize the select2:
Just put the following line code on my javascript and it does successful:
$('#category').select2();
:: How I'm trying to select a specific value:
First attempt:
$('#category').select2(
{
initSelection: function(element, callback) {
callback($('#field-category').val());
}
}
);
Second attempt:
$('#category').val($('#field-category').val());
P.S.: #field-category
has a value its a hidden input field and works OK.
Thanks, guys!
jquery-select2
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
@Ito See my updated answer, looks like no need to useinitSelection
– Arun P Johny
Feb 19 '13 at 12:37
add a comment |
Select2 loads all items from my list successful, the issue I found when try to select a specific value when page loads. Example:
:: put select2 in a specific html element, no value is selected even all items are loaded.
$('#my_id').select2();
:: When the page is loaded I'm trying to show a specific item selected, but doesn't work as expected, because even selected, the select2 doesn't show it.
$('#my_id').val('3'); //select the right option, but doesn't render it on page loads.
How to make a selected option to pop up when pages loads?
Thanks in advance.
UPDATED
:: How I load all select2 items (sorry, its jade, not pure HTML):
label(for='category') Category
span.required *
select(id='category', style='width:230px', name='category')
option(value='') - Select -
each cat in categories
option(value='#{cat.id}') #{cat.description}
P.S.: All items from my list are loaded.
:: How I initialize the select2:
Just put the following line code on my javascript and it does successful:
$('#category').select2();
:: How I'm trying to select a specific value:
First attempt:
$('#category').select2(
{
initSelection: function(element, callback) {
callback($('#field-category').val());
}
}
);
Second attempt:
$('#category').val($('#field-category').val());
P.S.: #field-category
has a value its a hidden input field and works OK.
Thanks, guys!
jquery-select2
Select2 loads all items from my list successful, the issue I found when try to select a specific value when page loads. Example:
:: put select2 in a specific html element, no value is selected even all items are loaded.
$('#my_id').select2();
:: When the page is loaded I'm trying to show a specific item selected, but doesn't work as expected, because even selected, the select2 doesn't show it.
$('#my_id').val('3'); //select the right option, but doesn't render it on page loads.
How to make a selected option to pop up when pages loads?
Thanks in advance.
UPDATED
:: How I load all select2 items (sorry, its jade, not pure HTML):
label(for='category') Category
span.required *
select(id='category', style='width:230px', name='category')
option(value='') - Select -
each cat in categories
option(value='#{cat.id}') #{cat.description}
P.S.: All items from my list are loaded.
:: How I initialize the select2:
Just put the following line code on my javascript and it does successful:
$('#category').select2();
:: How I'm trying to select a specific value:
First attempt:
$('#category').select2(
{
initSelection: function(element, callback) {
callback($('#field-category').val());
}
}
);
Second attempt:
$('#category').val($('#field-category').val());
P.S.: #field-category
has a value its a hidden input field and works OK.
Thanks, guys!
jquery-select2
jquery-select2
edited Jul 14 '13 at 0:11
JJD
25.8k36155258
25.8k36155258
asked Feb 19 '13 at 12:23
ItoIto
1,35931628
1,35931628
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
@Ito See my updated answer, looks like no need to useinitSelection
– Arun P Johny
Feb 19 '13 at 12:37
add a comment |
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
@Ito See my updated answer, looks like no need to useinitSelection
– Arun P Johny
Feb 19 '13 at 12:37
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
@Ito See my updated answer, looks like no need to use
initSelection
– Arun P Johny
Feb 19 '13 at 12:37
@Ito See my updated answer, looks like no need to use
initSelection
– Arun P Johny
Feb 19 '13 at 12:37
add a comment |
8 Answers
8
active
oldest
votes
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select
element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
|
show 7 more comments
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal<select>
.
– Kevin Brown
May 24 '15 at 0:32
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
add a comment |
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
add a comment |
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: @Kokizzu
val
isn't even a valid option for Select2, this works because calling.select2()
completely re-initializes Select2 (which ends up detecting the new selected value).
– Kevin Brown
May 24 '15 at 0:34
add a comment |
Here is how to make val
in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
1
maybe it's{val:3}
not{'val', 3}
?
– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and{val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.
– Kevin Brown
May 24 '15 at 0:36
add a comment |
I meet with the same problem,this works for me:
Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")
add a comment |
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
add a comment |
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14957419%2fselect2-doesnt-show-selected-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select
element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
|
show 7 more comments
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select
element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
|
show 7 more comments
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select
element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
You need to use the initSelection option to set the initial value.
If you are using a pre-defined select
element to create the select2, you can use the following method
$('select').select2().select2('val','3')
Demo: Fiddle
edited Feb 19 '13 at 12:36
answered Feb 19 '13 at 12:28
Arun P JohnyArun P Johny
327k51434450
327k51434450
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
|
show 7 more comments
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
I've tried initSelection before, maybe my code is wrong. I'll show here
– Ito
Feb 19 '13 at 12:29
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
if you can share the code, we can try
– Arun P Johny
Feb 19 '13 at 12:30
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Thanks Arun, worked for me. When page loads, select2 is loaded with a specific value.
– Ito
Feb 19 '13 at 12:43
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
Nice code, btw I want to understand why I have to use select2() twice?
– Ito
Feb 19 '13 at 12:44
2
2
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
This is now deprecated in favor of the other answer in Select2 4.0.0 and will likely stop working entirely in Select2 4.1.0.
– Kevin Brown
May 24 '15 at 0:33
|
show 7 more comments
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal<select>
.
– Kevin Brown
May 24 '15 at 0:32
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
add a comment |
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal<select>
.
– Kevin Brown
May 24 '15 at 0:32
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
add a comment |
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
add a trigger change after setting val:
$('#my_id').val('3').trigger('change');
answered Nov 11 '14 at 3:42
KokizzuKokizzu
11.3k1688159
11.3k1688159
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal<select>
.
– Kevin Brown
May 24 '15 at 0:32
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
add a comment |
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal<select>
.
– Kevin Brown
May 24 '15 at 0:32
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
3
3
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal
<select>
.– Kevin Brown
May 24 '15 at 0:32
This is now the recommended way of setting the value in 4.0.0, as it works consistently and matches a normal
<select>
.– Kevin Brown
May 24 '15 at 0:32
1
1
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
It worked for select2 :)
– Baqer Naqvi
Nov 6 '15 at 14:59
1
1
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
What if I don't want to trigger change and still have it show?
– bpromas
Nov 10 '15 at 12:45
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
But what if I've attached something to "select2:select" and not change? Doing the above doesnt work.
– dama_do_bling
Mar 8 '16 at 23:00
add a comment |
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
add a comment |
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
add a comment |
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
A very simple way to tackle this problem is :
//Step1: Here assuming id of your selectbox is my_id, so this will add selected attribute to 1st option
$('#my_id option').eq(0).prop('selected',true);
//Step2: Now reinitialize your select box
//This will work, if you haven't initialized selectbox
$('#my_id').select2();
or
//This will work, if you have initialized selectbox earlier, so destroy it first and initialise it
$('#my_id').select2('destroy').select2();
edited Oct 9 '18 at 6:43
mplungjan
90.1k22127184
90.1k22127184
answered Feb 13 '18 at 6:25
Raj SoniRaj Soni
493
493
add a comment |
add a comment |
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: @Kokizzu
val
isn't even a valid option for Select2, this works because calling.select2()
completely re-initializes Select2 (which ends up detecting the new selected value).
– Kevin Brown
May 24 '15 at 0:34
add a comment |
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: @Kokizzu
val
isn't even a valid option for Select2, this works because calling.select2()
completely re-initializes Select2 (which ends up detecting the new selected value).
– Kevin Brown
May 24 '15 at 0:34
add a comment |
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: @Kokizzu
Per here initSelection is deprecated in Select2 4.0 and later.
Using Select2 4.0.0 this worked for me:
$('#my_id').select2({val:3});
HT: @Kokizzu
answered May 4 '15 at 19:07
PatrickPatrick
4,86542020
4,86542020
val
isn't even a valid option for Select2, this works because calling.select2()
completely re-initializes Select2 (which ends up detecting the new selected value).
– Kevin Brown
May 24 '15 at 0:34
add a comment |
val
isn't even a valid option for Select2, this works because calling.select2()
completely re-initializes Select2 (which ends up detecting the new selected value).
– Kevin Brown
May 24 '15 at 0:34
val
isn't even a valid option for Select2, this works because calling .select2()
completely re-initializes Select2 (which ends up detecting the new selected value).– Kevin Brown
May 24 '15 at 0:34
val
isn't even a valid option for Select2, this works because calling .select2()
completely re-initializes Select2 (which ends up detecting the new selected value).– Kevin Brown
May 24 '15 at 0:34
add a comment |
Here is how to make val
in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
1
maybe it's{val:3}
not{'val', 3}
?
– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and{val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.
– Kevin Brown
May 24 '15 at 0:36
add a comment |
Here is how to make val
in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
1
maybe it's{val:3}
not{'val', 3}
?
– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and{val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.
– Kevin Brown
May 24 '15 at 0:36
add a comment |
Here is how to make val
in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
Here is how to make val
in select2 just select the corresponding element.
For some reason, select2 doesn't provide the function to look up selections by id.
init:
$("#thing").select2({data:sources, initSelection: function(item, callback) {
// despite select2 having already read the whole sources list when you
// do .val(n) you have to explicitly tell it how to find that item again.
var to_be_selected = null;
$.each(sources, function(index, thing) {
if (thing.id == item.val()) {
to_be_selected = thing;
return;
}
})
callback(to_be_selected);
}})
normal code
// to load the thing with id==3 from the initial sources list.
$("#thing").select2({'val': 3})
edited May 24 '15 at 7:46
answered Jun 23 '13 at 6:04
fastmultiplicationfastmultiplication
1,8862235
1,8862235
1
maybe it's{val:3}
not{'val', 3}
?
– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and{val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.
– Kevin Brown
May 24 '15 at 0:36
add a comment |
1
maybe it's{val:3}
not{'val', 3}
?
– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and{val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.
– Kevin Brown
May 24 '15 at 0:36
1
1
maybe it's
{val:3}
not {'val', 3}
?– Kokizzu
Feb 26 '15 at 13:36
maybe it's
{val:3}
not {'val', 3}
?– Kokizzu
Feb 26 '15 at 13:36
{val: 3}
(not a real option) and {val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.– Kevin Brown
May 24 '15 at 0:36
{val: 3}
(not a real option) and {val, 3}
(invalid syntax) won't actually work. You might get lucky if Select2 reinitializes itself, but that's a lucky side effect in the long run.– Kevin Brown
May 24 '15 at 0:36
add a comment |
I meet with the same problem,this works for me:
Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")
add a comment |
I meet with the same problem,this works for me:
Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")
add a comment |
I meet with the same problem,this works for me:
Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")
I meet with the same problem,this works for me:
Using Select2 4.0.0
$("#slect_id").val('3').trigger("change")
edited Jul 7 '16 at 4:59
Geeky Ninja
4,39762844
4,39762844
answered Jul 7 '16 at 3:31
mmarymmary
32
32
add a comment |
add a comment |
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
add a comment |
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
add a comment |
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
For me I was sending selected in the data set still default option was not getting selected. I had to do something like below to make it work -
$(".select").select2({
data: data_names
});
data_names.forEach(function(name) {
if (name.selected) {
$(".select").select2('val', name.id);
}
});
answered Dec 10 '17 at 16:35
Aniket ThakurAniket Thakur
43.1k25194220
43.1k25194220
add a comment |
add a comment |
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
add a comment |
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
add a comment |
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
This may help:
$('#mySelect2').val('1'); // Select the option with a value of '1'
$('#mySelect2').trigger('change'); // Notify any JS components that the value changed
You can find more on details here:
Thanks
answered Jan 3 at 2:18
sushil adhikarisushil adhikari
4215
4215
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f14957419%2fselect2-doesnt-show-selected-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Is "3" the value for the option that you want to select? Have you wrapped the code in a document ready function?
– Johan
Feb 19 '13 at 12:27
Can you share the mark up/data for the select2
– Arun P Johny
Feb 19 '13 at 12:30
@Ito See my updated answer, looks like no need to use
initSelection
– Arun P Johny
Feb 19 '13 at 12:37