How to insert data into Yii 2.0 extension CKEditor extension using JavaScript?

I installed the CKEditor Yii framework 2.0 extension with the following command.

php composer.phar require "2amigos/yii2-ckeditor-widget" "*" 

I use it in my form with the following code.

 use dosamigos\ckeditor\CKEditor; $form->field($myModel, 'text')->widget(CKEditor::className(), [ 'options' => ['rows' => 6], 'preset' => 'basic' ]); 

In addition, I have a dropdown in my form. Assume the following.

 <select id="select-number" class="form-control" name="MyModel[select-number]"> <option value="">-- Select a number --</option> <option value="1">Number 1</option> </select> 

When one selects an option, I want to paste some default data into the CKEditor text area. How can I select the CKEditor text box and paste data into it?

+4
source share
1 answer

In your view file, try the following snippet:

 <script> $('#select-number').on('change', function(){ var textareaID = "<?= '#' . Html::getInputId($model, 'your-attribute') ?>"; var data = $(this).find(':selected').text(); //or any other source of data //here we place data into editor instance CKEDITOR.instances[textareaID].setData(data); }) </script> 
+3
source

All Articles