I have a two dropdowns that use Select2.js
.
The second dropdown will initiate an ajax call base on the value from the first dropdown.
Assuming that I have already the result that I needed from Ajax call, and I already know what will be the result and that is the value 1276
and 1277
.
What I want is when I click either one of the results all the results will be selected.
As what I read in Neelu's answer this will supposed to do the trick:
//this code block is outside the ajax call of select2.js
$('.select-2').on('select2:select', function() {
$('.select-2').val([1276,1277]);
$('.select-2').trigger("change");
});
The result doesn't reflect in the Select2
element but when I check the value using .val()
, I get the result 1276
and 1277
.
What I noticed if I trigger a different event e.g. button click
to get the value of the Select2. That's the time it will work, but when I trigger another Select2 ajax call it doesn't work again.