4

I have installed 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'
]);

Further more, I have a dropdownlist in my form. Let's assume as follows.

<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 insert some default data into the CKEditor textarea. How can I select the CKEditor textarea and insert data into it?

1 Answer 1

3

In your view file try 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>
Sign up to request clarification or add additional context in comments.

1 Comment

got error Uncaught TypeError: Cannot read property 'setData' of undefined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.