69

I am binding my dropdown with Jquery-Select2. It's working fine but now I need to bind my Multi-Value selectBox by using Jquery-Select2.

My DropDown

<div class="divright">
  <select
    id="drp_Books_Ill_Illustrations"
    class="leaderMultiSelctdropdown Books_Illustrations"
    name="drp_Books_Ill_Illustrations"
    multiple=""
  >
    <option value=" ">No illustrations</option>
    <option value="a">Illustrations</option>
    <option value="b">Maps</option>
    <option value="c">Portraits</option>
  </select>
</div>

From this link http://ivaynberg.github.com/select2/ I am using Multiple Value Select Box, I can bind my dropdown with

$("dropdownid").Select2()

Its working fine, but now I need to get that selected value into my dropdown on edit mode So I am using this example:

$(".Books_Illustrations").select2("val", ["a", "c"]);

It's working but how can I fix my choice, because user can choose anything. So I can't write a,c statically that's why I need to bind my Selected value on Edit mode dynamically.

I think now you all are clear with my requirements. Please let me know if you need further clearance.

Mario Petrovic
  • 7,500
  • 14
  • 42
  • 62
Kaps Hasija
  • 2,097
  • 5
  • 23
  • 31

16 Answers16

69

Well actually your only need $.each to get all values, it will help you jsfiddle.net/NdQbw/5

<div class="divright">
            <select id="drp_Books_Ill_Illustrations" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
                <option value=" ">No illustrations</option>
                <option value="a" selected>Illustrations</option>
                <option value="b">Maps</option>
                <option value="c" selected>selectedPortraits</option>
            </select>
    </div>

<div class="divright">
        <select id="drp_Books_Ill_Illustrations1" class=" Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
            <option value=" ">No illustrations</option>
            <option value="a">Illustrations</option>
            <option value="b">Maps</option>
            <option value="c">selectedPortraits</option>
        </select>
</div>

<button class="getValue">Get Value</button>
<button  class="setValue"> Set  value </button>

<div class="divright">
        <select id="drp_Books_Ill_Illustrations2" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
            <option value=" ">No illustrations</option>
            <option value="a" selected>Illustrations</option>
            <option value="b">Maps</option>
            <option value="c" selected>selectedPortraits</option>
        </select>
</div>

<div class="divright">
        <select id="drp_Books_Ill_Illustrations3" class=" Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
            <option value=" ">No illustrations</option>
            <option value="a">Illustrations</option>
            <option value="b">Maps</option>
            <option value="c">selectedPortraits</option>
        </select>
</div>

<button class="getValue1">Get Value</button>
<button  class="setValue1"> Set  value </button>

The script:

 var selectedValues = new Array();
    selectedValues[0] = "a";
    selectedValues[1] = "c";

$(".getValue").click(function() {
    alert($(".leaderMultiSelctdropdown").val());
});
$(".setValue").click(function() {
   $(".Books_Illustrations").val(selectedValues);
});

$('#drp_Books_Ill_Illustrations2, #drp_Books_Ill_Illustrations3').select2();


$(".getValue1").click(function() {
    alert($(".leaderMultiSelctdropdown").val());
});

$(".setValue1").click(function() {
    //You need a id for set values
    $.each($(".Books_Illustrations"), function(){
            $(this).select2('val', selectedValues);
    });
});
jd_7
  • 1,852
  • 13
  • 19
  • actually thing is that we are getting value as a string not as a array so thats why i am using contain check to filter only selected value – Kaps Hasija Oct 25 '12 at 03:06
53

I get this post is old but there have been changes to how select2 works now and the answer to this question is extremely simple.

To set the values in a multi select2 is as follows

$('#Books_Illustrations').val([1,2,3]).change();

There is no need to specify .select2 in jquery anymore, simply .val

Also there will be times you will not want to fire the change event because you might have some other code that will execute which is what will happen if you use the method above so to get around that you can change the value without firing the change event like so

$('#Books_Illustrations').select2([1,2,3], null, false);

If that does not work you can also do this to not trigger the change event

$('#Books_Illustrations').select2();
$('#Books_Illustrations').val('some_value');
Cesar Bielich
  • 4,754
  • 9
  • 39
  • 81
22

So I take it you want 2 options default selected, and then get the value of it? If so:

http://jsfiddle.net/NdQbw/1/

<div class="divright">
    <select id="drp_Books_Ill_Illustrations" class="leaderMultiSelctdropdown Books_Illustrations" name="drp_Books_Ill_Illustrations" multiple="">
        <option value=" ">No illustrations</option>
        <option value="a" selected>Illustrations</option>
        <option value="b">Maps</option>
        <option value="c" selected>selectedPortraits</option>
    </select>
</div>

And to get value:

alert($(".leaderMultiSelctdropdown").val());

To set the value:

 $(".leaderMultiSelctdropdown").val(["a", "c"]);

You can also use an array to set the values:

var selectedValues = new Array();
selectedValues[0] = "a";
selectedValues[1] = "c";

$(".Books_Illustrations").val(selectedValues);

http://jsfiddle.net/NdQbw/4/

Geert
  • 1,217
  • 8
  • 20
15

Using select2 jquery library:

$('#selector').val(arrayOfValues).trigger('change')
Angie Alejo
  • 743
  • 9
  • 15
7
var valoresArea=VALUES
// it has the multiple values to set separated by comma
var arrayArea = valoresArea.split(',');
$('#area').select2('val',arrayArea);
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
pankaj yadav
  • 71
  • 1
  • 1
  • Thanks for your answer. However, it would be preferable if you could add a description as to why your code block solves the issue. This is a suggestion to improve this and future answers. Thanks! – Luís Cruz Oct 27 '15 at 14:18
7
This is with reference to the original question
$('select').val(['a','c']);
$('select').trigger('change');
Neelu
  • 251
  • 3
  • 6
4

Just look at this one it will helps.

var valoresArea=VALUES // it has the multiple values to set, separated by comma
var arrayArea = valoresArea.split(',');
$('#area').val(arrayArea);

the URL is- link

Ravichandran Jothi
  • 3,028
  • 11
  • 52
  • 82
4

you can add the selected values in an array and set it as the value for default selection

eg:

var selectedItems =[];
selectedItems.push("your selected items");
..
$('#drp_Books_Ill_Illustrations').select2('val',selectedItems );

Try this, this should definitely work!

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
Ravi Kiran
  • 41
  • 1
2

Using select2 library there are 2 ways to set the value 1. when single value is present you can pass it as string

$("#elementid").select2("val","valueTobeset")

2. when you are using select2 with multiselect option you can pass an array of the values to select2 and all those values will be set

var arrayOfValues = ["a","c"]
$("#elementid").select2("val",arrayOfValues)

remember that you can set single value on multiselect by passing an array also like this

var arrayOfValues = ["a"]
$("#elementid").select2("val",arrayOfValues)
Omkar Bandkar
  • 739
  • 6
  • 4
2

This doesn't work. only one value is ever pre-selected even though both options are available in the list only the first is shown ('#searchproject').select2('val', ['New Co-location','Expansion']);

djack109
  • 1,261
  • 1
  • 23
  • 42
2

Use multiselect function as below.

$("#drp_Books_Ill_Illustrations").val(["val1", "val2"]).trigger("change");

Waruna Manjula
  • 3,067
  • 1
  • 34
  • 33
2

To Select all

 $('select[name=eventsFilter]').find('option').attr('selected', true);
$('select[name=eventsFilter]').select2();

To UnSellect all

$('select[name=eventsFilter]').find('option').attr('selected', false);
  $('select[name=eventsFilter]').select2("");
ferozpuri
  • 266
  • 2
  • 9
0

no need to do so many things. for set value using multiple select2 var selectedvalue="1,2,3"; //if first 3 products are selected. $('#ddlProduct').val(selectedvalue);

0
$('#area').select2().val(arrayArea).trigger('change'); 

I used to like this and I am using the select2 version4. I got the result

Teresa
  • 127
  • 2
  • 8
0

This worked for me:

$(".Books_Illustrations").val(["a", "c"]).trigger('change');
jrreda
  • 763
  • 7
  • 7
-5

Use multiselect function as below.

$('#drp_Books_Ill_Illustrations').multiSelect('select', 'value');
MKB
  • 7,587
  • 9
  • 45
  • 71