Select2 Widge Reset Selected Value

2k Views Asked by At

How to reset selected value in yii2 widget select2 using jQuery or JavaScript? I've tried to use $("#select2id").val("").trigger("change"); and it show the placeholder, but the first item is always automatically selected.

EDIT: I'm using this widget for make dependency dropdown. the widget that the first item always automatically selected is the dependent widget.

expected result

2

There are 2 best solutions below

0
On BEST ANSWER

I've solved the problem. That code is actually working. The order of the code that make the problem. So, be sure to check the code order.

2
On

You should do it the select2 way:

$("#select2id").select2("val", "", true);

With the first two parameters you assign an empty string as a value and the last one is for triggering the change.