title: How to load a template dynamically in CKEDITOR by using ajax?
body: <p>I have a database table for my saved template. All are in HTML format and I want to get a specific template using an AJAX.</p>
  
  <p>Here's my code:</p>
  
  <p><strong>HTML part</strong></p>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;label&gt;Select Template: &lt;span class="text-info"&gt;(*Optional)&lt;/span&gt;&lt;/label&gt;
      &lt;select class="form-control" name="load_template" id="load-template"&gt;
          &lt;?php if(count($templates) &gt; 0) { ?&gt;
          &lt;option value=""&gt;-- load template --&lt;/option&gt;
          &lt;?php foreach($templates as $temp) { ?&gt;
              &lt;option value="&lt;?php echo $temp['id']; ?&gt;"&gt;&lt;?php echo $temp['template_name']; ?&gt;&lt;/option&gt;
          &lt;?php } ?&gt;
          &lt;?php } else { ?&gt;
              &lt;option value=""&gt;There are no saved templates&lt;/option&gt;
          &lt;?php } ?&gt;
      &lt;/select&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>JS part</strong></p>
  
  <pre><code>$('#editor').ckeditor();
  
  $('#load-template').on('change', function(){
      var id = $(this).val();
      //do some ajax to get content...
      console.log(id);
      $.ajax({
          url: "&lt;?php echo site_url('users/user/getLoadTemplate'); ?&gt;",
          data: {template_id: id},
          dataType: 'html',
          type: 'post',
          beforeSend: function() {},
          success: function(template) {
              $('#editor').html(template);
              console.log(template); //ok it shows the HTML but how can I load it in my ckeditor?
          },
          error: function() {
              alert('Unable to load template. Error in AJAX');
          }
      });
  });
  </code></pre>
  
  <p><strong>PHP side</strong></p>
  
  <pre><code>public function getLoadTemplate() {
      $type = $this-&gt;input-&gt;post('template_id');
      $get_template = $this-&gt;Users_model-&gt;getLoadTemplate($type);
      echo $get_template['template'];
  }
  </code></pre>
  
  <p>How can I load my ajax response HTML in the CKEDITOR?</p>