2

Want to change selected value of dependent drop-down. After loading drop-down values with ajax in dependent drop down following code is not working

$("#dependentDropDownID").select2("val", data);

Above code working fine on first time load of dependent drop-down.

vinod
  • 2,850
  • 1
  • 18
  • 23
priti narang
  • 258
  • 4
  • 18
  • 1
    Please check http://stackoverflow.com/help/mcve to see How to create a Minimal, Complete, and Verifiable example – pringi Jan 27 '17 at 11:00
  • Please follow pringi's advice. And please specify "code is not working". Are there error messages? What is the result, what is the desired result? – gus27 Jan 27 '17 at 11:53

2 Answers2

3

When we are using kartik select2 drop down then actual id of drop down gone in hidden state so to change value of hidden drop down we have to use "change" event on that id. Use following code for changing selected value of drop down.

$("#dependentDropDownID").val(data).change();
vinod
  • 2,850
  • 1
  • 18
  • 23
0

Kartik prepared plugin called DepDrop, which will do all for you.

echo $form->field($model, 'subcat')->widget(DepDrop::classname(), [
   'options'=>['id'=>'subcat-id'],
   'pluginOptions'=>[
       'depends'=>['cat-id'],       // id of first input
       'placeholder'=>'Select...',
       'url'=>Url::to(['/site/subcat'])   //url to fetch data
   ]
]);

Plugin: Kartik DepDrop

Yupik
  • 4,932
  • 1
  • 12
  • 26