Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Select2 with a selected option WITHOUT triggering change?

Is there any way to load select2 to a default value without triggering the change event?

Currently, I am doing:

$('#mylist').val(2).trigger("change");

But this creates a delay where the user will at first see one option and then it will change, which is quite an annoyance.

like image 212
space_food_ Avatar asked Nov 28 '22 07:11

space_food_


1 Answers

I know I'm really late to answer this question. I was facing the same issue. But doing a bit of research I found a solution that works like a charm.

You can set an option using below code

$("#mylist").val(2).trigger('change.select2');

This solution will not trigger the main change event but sets the option to the select2 component.

Hope that helps you.

like image 121
BEingprabhU Avatar answered Dec 05 '22 23:12

BEingprabhU