71

I am using Select2 Jquery to bind my dropdown which is used for multiple selection . I am using select2 jquery.

It's working fine, I can bind my dropdown but I need to get the selected value from my multi-value selector. I am looking for method to get value which is supported by select2 Jquery. it might be having a function get selected value.
my drop down binding code

$(".leaderMultiSelctdropdown").select2( {
    maximumSelectionSize: 4
});
Rich
  • 5,603
  • 9
  • 39
  • 61
Kaps Hasija
  • 2,097
  • 5
  • 23
  • 31

9 Answers9

117
alert("Selected value is: "+$(".leaderMultiSelctdropdown").select2("val"));

alternatively, if you used a regular selectbox as base, you should be able to use the normal jquery call, too:

alert("Selected value is: "+$(".leaderMultiSelctdropdown").val());

both return an array of the selected keys.

Manuel Schweigert
  • 4,884
  • 4
  • 20
  • 32
40

I know its late but I think you can try like this

$("#multipledpdwn").on("select2:select select2:unselect", function (e) {

    //this returns all the selected item
    var items= $(this).val();       

    //Gets the last selected item
    var lastSelectedItem = e.params.data.id;

})

Hope it may help some one in future.

ksg
  • 3,927
  • 7
  • 51
  • 97
28

Returns the selected data in structure of object:

console.log($(".leaderMultiSelctdropdown").select2('data'));

Something like:

   [{id:"1",text:"Text",disabled:false,selected:true},{id:"2",text:"Text2",disabled:false,selected:true}]

Returns the selected val:

console.log($('.leaderMultiSelctdropdown').val());
console.log($('.leaderMultiSelctdropdown').select2("val"));

Something like:

["1", "2"]
JaskeyLam
  • 15,405
  • 21
  • 114
  • 149
11

Simply :

$(".leaderMultiSelctdropdown").val()
Amro Mustafa
  • 589
  • 4
  • 15
8

Try like this,

jQuery('.leaderMultiSelctdropdown').select2('data');
Zeeshan
  • 1,659
  • 13
  • 17
user4310702
  • 89
  • 1
  • 1
  • 1
    The currently accepted answer did not work for me - it just gave `["1","2"]`, etc. no matter what I picked. This one gave the correct info for me. – user3413723 Oct 31 '15 at 02:44
4

You should try this code.

 $("#multiple_Package_Ids_checkboxes").on('change', function (e) { 
        var totAmt = 0;
        $.each($(this).find(":selected"), function (i, item) { 
            totAmt += $(item).data("price");
            });
        $("#PackTotAmt").text(totAmt);
    }); 
Deepak Jha
  • 359
  • 2
  • 10
1

Try this:

  $('.select').on('select2:selecting select2:unselecting', function(e) {

      var value = e.params.args.data.id;

  });
RGL
  • 71
  • 1
  • 11
  • 1
    You might want to expand a bit on why this answer is different from the other ones. – rene Jan 12 '17 at 21:26
1

Please, ckeck this simple example. You can get values in select2 multi.

var values = $('#id-select2-multi').val();
console.log(values);
Alberto Cerqueira
  • 1,339
  • 14
  • 18
-3

This will get selected value from multi-value select boxes: $("#id option:selected").val()