Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

218
Vistas
Can't pre-select options in Select2 multiple choice select box

I use the select2 jQuery module to create a multiple choice select box. I did this in an early version (3.4.5) and it worked fine. But after upgrade to 4.1.0 pre-selection of an option does not work.

My code looks like below,

HTML - part:

<input type="text" id="accessories" />

JS:

jQuery(document).ready(function() {
   var acceArray2 = [{id:0,text:"textA"},{id:1,text:"textB"},{id:2,text:"textC"}];
        
   jQuery("#accessories").select2({
                        data: acceArray2,
                        multiple: true,
                        placeholder: "",
                        width: 200
                    });
   jQuery("#accessories").val("1");
   jQuery("#accessories").trigger("change");
});

My options show up when I click in the box. I can also select one or more option. But I expect "textB" should be selected when I open the page. This works fine with the original select2 version, but not with 4.1.0. The box is just empty.

I have spent hours trying to figure what's wrong, but I can't....

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Alternatively, you can change the input tag to select. It seems to work below:

jQuery(document).ready(function() {
  const acceArray2 = [{
    id: 0,
    text: "textA"
  }, {
    id: 1,
    text: "textB"
  }, {
    id: 2,
    text: "textC"
  }];

  jQuery("#accessories").select2({
    data: acceArray2,
    multiple: true,
    placeholder: "",
    width: 200
  });
  jQuery("#accessories").val("1");
  jQuery("#accessories").trigger("change");

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>

<select id="accessories"></select>

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can add selected option in array object like:

jQuery(document).ready(function() {
  var acceArray2 = [{
    id: 0,
    text: "textA"
  }, {
    id: 1,
    text: "textB",
    selected: true
  }, {
    id: 2,
    text: "textC",
    selected: true
  }];

  jQuery("#accessories").select2({
    data: acceArray2,
    multiple: true,
    placeholder: "",
    width: 200
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<input type="text" id="accessories" />

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda