2

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?

Paul
  • 1,630
  • 1
  • 16
  • 23
snew
  • 25
  • 1
  • 4

1 Answers1

5

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);
});
gmc
  • 3,910
  • 2
  • 31
  • 44