How can I set form values with Ajax? I want to set the value of a textfield based upon the selection of a field above. You can choose a template and the textfield would have it as a value. 

This is the select field used to select the template with the ajax call

// Template Select field
$form['template'] = [
'#type' =&gt; 'select',
'#title' =&gt; $this-&gt;t('Template'),
'#options' =&gt; $event_templates_select,
'#required' =&gt; TRUE,
'#ajax' =&gt; [
    'callback' =&gt; '::changeOutputValue',
    'disable-refocus' =&gt; FALSE,
    element.
        'event' =&gt; 'change',
        'wrapper' =&gt; 'edit-output',
        'progress' =&gt;[
            'type' =&gt; 'throbber',
            'message' =&gt; $this-&gt;t('Updating e-mail template'),
        ],
    ]
];


This is the textfield that I want to display the value. It's in a ckeditor.

// Textarea field
$form['output'] = [
'#type' =&gt; 'text_format',
'#format' =&gt; 'full_html',
'#required' =&gt; TRUE,
'#disabled' =&gt; FALSE,
'#value' =&gt; 'hallo',
'#prefix' =&gt; '&lt;div id="edit-output" class="acceptform__output"&gt;',
'#suffix' =&gt; '&lt;/div&gt;',
];


And finally, the ajax function

  public function changeOutputValue(array &amp;$form, FormStateInterface $form_state) {

    $selected_option =  $form_state-&gt;getValue('template');
    if ($selected_option == 0){
        $output_text = '';
    } else {
        $template = \Drupal::entityTypeManager()-&gt;getStorage('taxonomy_term')-&gt;load($selected_option);
        $tempate_content = $template-&gt;get('field_template')-&gt;getValue();
        $tempate_content = $tempate_content[0]['value'];
        $output_text = $tempate_content;
    }


    $form['output']['#value'] = $output_text;

    $response = new AjaxResponse();
    return $response;

  }   
}


Can someone explain what I'm doing wrong. I was trying to follow the drupal 8 form api docs but I can't get it to work with a ckeditor field.

The select field contains a number of e-mail templates. Upon selecting one, I want to display the template in the output field with a ckeditor so the user can still make changes if necessary. 

I'm using Drupal 8 with a custom module.

Thanks in advance.
From the ajax callback, you only need to return the render array containing the data to be inserted into the div you have specified as the callback to your #ajax.

So in the ajax callback you can return this, rather than an AjaxResponse:

return $form['output'];