1

Is it possible to set certain options as selected in yii2 select2 widget from jquery. The widget is not initialised through js but it is bound to model attribute in form. Any help would be appreciated

Code:

echo $form->field($model, 'news_tags')->widget(Select2::classname(), [
                    'data' => ArrayHelper::map(app\models\Topics::findAll(['status' => 1]), 'tp_id', 'title'),
                    'options' => ['multiple' => true, 'placeholder' => 'Select Tags ...'],
                    'pluginOptions' => [
                        'tags' => false,
                        'allowClear' => true
                    ],
                ])->label(FALSE);

I am trying to use an ajax call and on succes i want to add some tags from clientside.

8
  • Please share your code or fiddle Commented Jul 29, 2016 at 10:59
  • Are you using kartik select2? Commented Jul 29, 2016 at 12:29
  • It is possible.put your codes Commented Jul 29, 2016 at 19:43
  • Sorry for being late! I'll put the codes rightaway Commented Aug 1, 2016 at 4:52
  • Yes! I am using kartik select2. Commented Aug 1, 2016 at 5:01

1 Answer 1

4

As is presented in docs https://select2.github.io/examples.html#programmatic you can use following code:

$("#form-field").val("5").trigger("change");

and it will select option, that has id 5

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.