1

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!

1
  • $("#select_custom option[value='cty01']").text() create loop for all value. Commented Oct 22, 2021 at 13:43

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error on my first select: Uncaught Error: Syntax error, unrecognized expression: [option value="cty03"]
With this correction it's working: var label = select_custom.find('option[value="'+values[i]+'"]').text();
@Dali sorry, didn’t notice the typo. Have updated my answer. Glad you figured out what was wrong!
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>

7 Comments

hello, I need an array of texts, not values
@Dali as you can see the code you have the text not the values, so to be clear you want to push all the text options when you change the select or you want to push te text of option which is selected ?
oh sorry, I can see that now. I will try this. (I want to push the text of options selected only)
@Dali okay then I'm changing my code just a minute
@Dali the code its done.
|
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>

5 Comments

I don't think 'option:selected' will work with Chosen.
Did you tried ? @Dali
Yes it gives me a string like this: MadridMontrealParisBerlin
You can try Chosen Multiple Select here: harvesthq.github.io/chosen you will see that there is no Selected if you inspect.
But I see now that there is a 'class="result-selected', maybe I can use that...
0

One line to get an array of selected texts or values

var array_of_texts = $('#Your_multiple_select option:selected').toArray().map(item => item.text);

Output: ["text1", "text2"]

var array_of_values = $('#SelectQButton option:selected').toArray().map(item => item.value);

Output: ["value1", "value2"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.