how to change select2 value programmatically
29,992
Solution 1
$('#select_id').val('val').trigger('change');
is the right way, see here
Solution 2
$('#select_id').select2('val', selectedValue);
Solution 3
For Select2 with Ajax call, I struggled with lot of options, but none worked. Then i came to following solution, which works like charm $("#select_id").html($("").val('val').text('text')).trigger("change");
Author by
Chaanbi Kada
Updated on October 14, 2021Comments
-
Chaanbi Kada over 2 years
I have a select2 box in bootstrap modal, I want to change the value of the select2 box but it didn't work.
I tried every single solution in previous posts and results but none of them got it work.
I use select2 4.0.2, I tested:
$('#select_id').val('val').trigger('change.select2'); $('#select_id').val('val').trigger('change'); $('#select_id').val('val').change()
It works one or two times then it stops working (randomly)
It works fine only when I change the select2 back to 3.x.x