3

I am using the SumoSelect dropdown for multiselect options. But i cannot get the selected values array. Below the sample code :

<script type="text/javascript">
    $(document).ready(function () {         
    window.testSelAll = $('.testSelAll').SumoSelect({okCancelInMulti:true, selectAll:true });           

        $('.btnOk').on('click', function(){
            var obj = [];
            $('option:selected').each(function () {
                obj.push($(this).index());  
                alert("Selected Values=="+$(this).val());   
            });

            for (var i = 0; i < obj.length; i++) {                      
                $('.testSelAll')[0].sumo.unSelectItem(obj[i]);
            }
        });                 
    });

</script>

<select multiple="multiple" placeholder="Share Your Friends" onchange="console.log($(this).children(':selected').length)" class="testSelAll">
                <option value="1">Volvo</option>
               <option value="2">Saab</option>
               <option  value="3">Mercedes</option>
               <option value="audi">Audi</option>
               <option value="bmw">BMW</option>
               <option value="porsche">Porche</option>
               <option value="ferrari">Ferrari</option>
               <option value="mitsubishi">Mitsubishi</option>
       </select>
Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
Ramalingam Perumal
  • 1,367
  • 2
  • 17
  • 46

3 Answers3

6

If you want the selected values instead of the text, just change .text() to .val().

If you want to get the array, see below with working example at the bottom.

jQuery

$(document).ready(function() {
  $('.testSelAll').SumoSelect({
    okCancelInMulti: true,
    selectAll: true
  });

  $('.btnOk').on('click', function() {
    var obj = [],
        items = '';
    $('.testSelAll option:selected').each(function(i) {
      obj.push($(this).val());
      $('.testSelAll')[0].sumo.unSelectItem(i);
    });
    for (var i = 0; i < obj.length; i++) {
      items += ' ' + obj[i]
    };
    alert(items);
  });
});

HTML

<select multiple="multiple" class="testSelAll">
  <option value="car1">Volvo</option>
  <option value="car2">Saab</option>
  <option value="car3">Mercedes</option>
  <option value="car4">Audi</option>
</select>

Working JSFIDDLE

Magnus Engdal
  • 5,446
  • 3
  • 31
  • 50
3

You can get them from underlying hidden select element. using jquery eg.

$('.select1 option:selected')
brandelizer
  • 342
  • 3
  • 17
2

I think the cleanest way to do this. Is to take advantage of html5 select element underlying SumoSelect.

HTML

<select multiple="multiple" class="testSelAll" id="multi-select">
  <option value="car1">Volvo</option>
  <option value="car2">Saab</option>
  <option value="car3">Mercedes</option>
  <option value="car4">Audi</option>
</select>

Javascript

var values = $('#multi-select').val();

This line will return a string list of the values selected.

Mazen Elkashef
  • 3,430
  • 6
  • 44
  • 72