4

I want to select multiple options in a select2 multivalue control, when clicking a button. This is useful specially for searches where the user needs to select many option to one of the filters

any idea how to achieve it..? I found how to select an option in select2 single option dropdown, but not in a multiselect control for several options at the same time

2 Answers 2

6

Ok, I found the way. It's just passing an array of option values to 'val' argument:

$('#my_select2').select2('val', ["value1", "value2", "value3"]);
1
  • an anonymous users just posted this as an alternative solution: var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data.text, data.id, false, false); $('#mySelect2').append(newOption).trigger('change'); this solution is referenced from select2.org/programmatic-control/…
    – luciano_s
    Commented Nov 7, 2019 at 16:20
1

you can also do it this way by defining javascript array.

var Values = new Array();
Values.push("value1");
Values.push("value2");
Values.push("value3");

$("#my_select2").val(Values).trigger('change');

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.