snew
snew

Reputation: 25

How to get value of selected option of yii2 select2 widget

I am trying to get the value of selected option within same page. I am using select2 widget.

<?php
echo '<label class="control-label">Group</label>';
$data = ['1' => 'option1', '2' => 'option2'];
echo Select2::widget([
    'name'=> 'group',
    'data' => $data,
    'options' => ['placeholder' => 'Select a option...', 'id' => 'sem'],
    'pluginOptions' => [
        'allowClear' => true
    ],
]);
?>
<input type="text" id='items' value="<?=$selectedvalue?> />'

Here I want get that selected value within that page in input box.

<input type="text" id='items' value="<?=$selectedvalue?> />

I am using Yii2 I am new on it. can anyone help me?

Upvotes: 2

Views: 6561

Answers (1)

gmc
gmc

Reputation: 3990

You can get the selected value with jQuery with:

$("#id_of_your_select2_widget").on("change", function (e) { 
    var id = $("#id_of_your_select2_widget").select2("data")[0].id;

   // Now you can work with the selected value, i.e.:
   $("#items").val(id);
});

Upvotes: 5

Related Questions