how to display options depending on the selected field?


My database:


id | name


id | cat_id | name

And I would like to show a drop-down list of subcategories depending on the selected categories, how to do it?

    <?= $form->field($model, 'categories')->dropDownList(


		['prompt'=>'Select category']

	) ?>

This might be a help.

OK, I used this module

And this is my code:


<?= $form->field($model, 'categories')->dropDownList($catList, ['id'=>'cat_name', 'prompt'=>'Select category']);?>


    <?= $form->field($model, 'subcategories')->widget(DepDrop::classname(), [




		'placeholder'=>'Select subcategory',

		'url'=>  Url::to(['ad/subcat'])


		]);  ?>

and controller:

	public function actionSubcat() {

        $out = [];

        if (isset($_POST['depdrop_parents'])) {

        $parents = $_POST['depdrop_parents'];

        if ($parents != null) {

        $id = $parents[0];



       ->select(['id','sub_name AS name'])->asArray()->all();

        echo Json::encode(['output'=>$out, 'selected'=>'']);




        echo Json::encode(['output'=>'', 'selected'=>'']);


And it works fine, but I would like to show or hide DIV depending on the selected subcategory.

And this is my code:

    <?php $this->registerJs(' $(\'select[id="sub_name"]\').change(function(){


    if ($(\'option:selected\').val() == 3)


       document.getElementById("table").style.display = "block";


       document.getElementById("table").style.display = "none";


    });', View::POS_READY);


But it does not work, it takes the value from category dropdown list.

How do I get the value from the selected sub-category?

That’s probably because you are using a general selector $(\‘option:selected\’).val(),

[size=2]try with $(\’#sub_name option:selected\’).val()[/size]

It works! Thanks <_<