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;
}







33















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!










share|improve this question

























  • 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




















33















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!










share|improve this question

























  • 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
















33












33








33


7






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!










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 use initSelection

    – 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











  • 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



















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














8 Answers
8






active

oldest

votes


















49














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






share|improve this answer


























  • 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



















26














add a trigger change after setting val:



$('#my_id').val('3').trigger('change');





share|improve this answer



















  • 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



















2














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();





share|improve this answer

































    0














    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






    share|improve this answer
























    • 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



















    0














    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})





    share|improve this answer





















    • 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



















    0














    I meet with the same problem,this works for me:



    Using Select2 4.0.0
    $("#slect_id").val('3').trigger("change")





    share|improve this answer

































      0














      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);
      }
      });





      share|improve this answer































        0














        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






        share|improve this answer
























          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          49














          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






          share|improve this answer


























          • 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
















          49














          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






          share|improve this answer


























          • 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














          49












          49








          49







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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













          26














          add a trigger change after setting val:



          $('#my_id').val('3').trigger('change');





          share|improve this answer



















          • 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
















          26














          add a trigger change after setting val:



          $('#my_id').val('3').trigger('change');





          share|improve this answer



















          • 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














          26












          26








          26







          add a trigger change after setting val:



          $('#my_id').val('3').trigger('change');





          share|improve this answer













          add a trigger change after setting val:



          $('#my_id').val('3').trigger('change');






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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














          • 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











          2














          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();





          share|improve this answer






























            2














            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();





            share|improve this answer




























              2












              2








              2







              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();





              share|improve this answer















              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();






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Oct 9 '18 at 6:43









              mplungjan

              90.1k22127184




              90.1k22127184










              answered Feb 13 '18 at 6:25









              Raj SoniRaj Soni

              493




              493























                  0














                  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






                  share|improve this answer
























                  • 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
















                  0














                  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






                  share|improve this answer
























                  • 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














                  0












                  0








                  0







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  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



















                  • 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











                  0














                  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})





                  share|improve this answer





















                  • 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
















                  0














                  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})





                  share|improve this answer





















                  • 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














                  0












                  0








                  0







                  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})





                  share|improve this answer















                  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})






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  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














                  • 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











                  0














                  I meet with the same problem,this works for me:



                  Using Select2 4.0.0
                  $("#slect_id").val('3').trigger("change")





                  share|improve this answer






























                    0














                    I meet with the same problem,this works for me:



                    Using Select2 4.0.0
                    $("#slect_id").val('3').trigger("change")





                    share|improve this answer




























                      0












                      0








                      0







                      I meet with the same problem,this works for me:



                      Using Select2 4.0.0
                      $("#slect_id").val('3').trigger("change")





                      share|improve this answer















                      I meet with the same problem,this works for me:



                      Using Select2 4.0.0
                      $("#slect_id").val('3').trigger("change")






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 7 '16 at 4:59









                      Geeky Ninja

                      4,39762844




                      4,39762844










                      answered Jul 7 '16 at 3:31









                      mmarymmary

                      32




                      32























                          0














                          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);
                          }
                          });





                          share|improve this answer




























                            0














                            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);
                            }
                            });





                            share|improve this answer


























                              0












                              0








                              0







                              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);
                              }
                              });





                              share|improve this answer













                              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);
                              }
                              });






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Dec 10 '17 at 16:35









                              Aniket ThakurAniket Thakur

                              43.1k25194220




                              43.1k25194220























                                  0














                                  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






                                  share|improve this answer




























                                    0














                                    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






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      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






                                      share|improve this answer













                                      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







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 3 at 2:18









                                      sushil adhikarisushil adhikari

                                      4215




                                      4215






























                                          draft saved

                                          draft discarded




















































                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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

                                          'app-layout' is not a known element: how to share Component with different Modules

                                          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                          WPF add header to Image with URL pettitions [duplicate]