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

162
Vistas
jQuery Chosen : How to get array of selected options text from a multiple select

I'm using jQuery Chosen and I need to put every option text in an array from a multiple select's dropdown.

I can easily get all values in an array with this:

    <select id="select_custom" data-placeholder="Choose a city" style="width:50%;" class="chosen-processed" multiple>
      <option value="cty01">New York</option>
      <option value="cty02">Madrid</option>
      <option value="cty03">Montreal</option>
      <option value="cty04">Paris</option>
      <option value="cty05">Berlin</option>
    </select>

   var select_custom = jQuery("#select_custom");

   select_custom.change(function(){
    var values = select_custom.chosen().val();
   });

And depending on what option is selected, it will return an array of values like this:

    ['cty01', 'cty03', 'cty04']

What would be the equivalent of this to get the text instead of the value?

    ['New York', 'Montreal', 'Paris']

Thanks in advance for your help!

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

0

var select_custom = jQuery("#select_custom");

select_custom.change(function(){
  var values = select_custom.chosen().val();

  var labels = [];

  for (var i = 0; i < values.length; i++) {
    var label = select_custom.find('option[value="'+values[i]+'"]').text();
    labels.push(label);
  }

  console.log(labels);

});

We're basically looping through each of the values, searching the <select> field for <option> tags matching the value and then pushing it to the labels array.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use map function

https://api.jquery.com/jquery.map/

$("#select_custom").change(function() {
  var text = $(this).find('option:selected').toArray().map(item => item.text).join();
  console.clear();
  console.log(text);
  $("#result").text(text);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="select_custom" data-placeholder="Choose a city" style="width:50%;" class="chosen-processed" multiple>
      <option value="cty01">New York</option>
      <option value="cty02">Madrid</option>
      <option value="cty03">Montreal</option>
      <option value="cty04">Paris</option>
      <option value="cty05">Berlin</option>
    </select>
    
<div id="result"></div>

about 4 years ago · Juan Pablo Isaza Denunciar

0

var select_custom = jQuery("#select_custom");
var arr = [];

select_custom.change(function(){
  var text = $('#select_custom option:selected').text();
  if(!arr.includes(text)) {
    arr.push(text);
  }
  console.log(arr);  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<select id="select_custom" data-placeholder="Choose a city" style="width:50%;" class="chosen-processed" multiple>
    <option value="cty01">New York</option>
    <option value="cty02">Madrid</option>
    <option value="cty03">Montreal</option>
    <option value="cty04">Paris</option>
    <option value="cty05">Berlin</option>
</select>

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