Reputation: 25
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
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