28

I have problem in get all selected option in multi select

<select multiple="" title="" class="" id="fm_delivery_or_collection" name="fm_fields[fm_delivery_or_collection][]">
  <option value="90">Delivery or Collection1</option>
  <option value="91">Delivery or Collection2</option>
  <option value="92">Delivery or Collection3</option>
</select>

Bellow is my code and its return me only first selected option

var select = form.find('select')

for (var i = 0; i < select.length; i++) 
        {
            var s_id = jQuery(select[i]).attr('id');
            var str="",i;

            var e = document.getElementById(s_id);
            var strUser = e.options[e.selectedIndex].text;

            var name = jQuery(select[i]).attr('name')
            var str1 = jQuery(select[i]).attr('id').replace("fm_"," ")
            requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>";
        }

So please suggest me how can i get all selected option text and where I make mistake ?

2
  • The value of select multiple (or select[select.selectedIndex]) is an array... Commented Feb 20, 2013 at 5:15
  • 2
    The answers are overly complicated. You can get all selected options text using .map(). $("select :selected").map(function (i, element) { return jQuery(element).text(); }).get(); Commented Jan 30, 2018 at 10:08

7 Answers 7

51

Your comment please suggest me how can i get all selected option text, So you can try this:

$("#fm_delivery_or_collection option:selected").each(function () {
   var $this = $(this);
   if ($this.length) {
    var selText = $this.text();
    console.log(selText);
   }
});
Sign up to request clarification or add additional context in comments.

Comments

39

do all in one line now

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

it return like this:

text1,text2,text3,text4

1 Comment

Nice and simple solution. If you need a different output, join() accepts a separator as an argument. For exemple join(' | '), will ouput "text1 | text2 | text3 | text4". And a slight improvement : jQuery recommands using $('#selector').find('option:selected'), because searching by ID alone relies on document.getElementById, which is very fast, while the full selector does not. If #selector refers to a select tag, you may also remove the unnecessary "option" from the selector for a bit more efficiency : learn.jquery.com/performance/optimize-selectors
13
// Return an array of the selected opion values
// select is an HTML select element
function GetSelectValues(select) {
  var result = [];
  var options = select && select.options;
  var opt;

  for (var i=0, iLen=options.length; i<iLen; i++) {
    opt = options[i];

    if (opt.selected) {
      result.push(opt.value || opt.text);
    }
  }
  return result;
}

pass this function your select box as like..

var selectBox = document.getElementsById('fm_delivery_or_collection');
alert(GetSelectValues(selectBox ));

it will return the array of selected values

By using Jquery

$('select#fm_delivery_or_collection').val()

the val function called from the select will return an array if its a multiple.

Comments

2

Try with this

$("#fm_delivery_or_collection option").each(function(){
    if($(this).attr('selected') == 'selected')
    {
        var name = $("#fm_delivery_or_collection").attr('name')
        var str1 = $("#fm_delivery_or_collection").attr('id').replace("fm_"," ")
        requestString += "<b>"+str1.replace(/_/g," ")+"</b>" + ':' +strUser+"<br>";
     }
})

3 Comments

see your iteration you are iterating through options and options doesn't have id and name. Can you see that?
Well you can add .parent() in that, try with it.
Oh yes you are right @Jai...thanx for suggestion...I have edited now
2
 $("#id :selected").each(function (i,sel) {
   alert($(sel).text());
 });

Hope it Helps..

Comments

2

try this link:

$("#selection option:selected").each(function () {
   var $this = $(this);
   if ($this.length) {
    var selText = $this.text();
    console.log(selText);
    $('#selected').append(selText+', ');
  }
});

Comments

2

Simply add this line of code:-

var reciever= jQuery('#to_select option:selected').toArray().map(item => item.text).join();
/* alert(reciever); */

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.