How can I append text to html source in CKEditor?

雨燕双飞 提交于 2019-12-03 13:32:48

If you are trying to append HTML text, you could use the createFromHtml method like this for example:

var imgHtml = CKEDITOR.dom.element.createFromHtml("<img src=" + imageSrcUrl + " alt='' align='right'/>");

where imageSrcUrl is the image location and then you can insert it into the ckeditor source like this:

CKEDITOR.instances.body.insertElement(imgHtml);

There are other methods like insertHtml or insertText, you can check the CKEditor APIs for more details on these.

According to this post http://www.techsirius.com/2013/09/dynamically-insert-string-into-ckeditor.html

You can insert text into ckeditor(textarea). You just need to give an unique ID to ckeditor(textarea) after that follow below code.

<script type=”text/javascript”>
  function insertIntoCkeditor(str){
    CKEDITOR.instances[ckeditor_id].insertText(str);
  }
</script>

This is working demo link. http://demo.techsirius.com/demo/dynamically-insert-string-into-ckeditor

Other sample working function :


  function insertIntoCkeditor(str,url){

  var tagHtml = ''+str+'';

    //CKEDITOR.instances['bilgi'].insertText(tagHtml);
    CKEDITOR.instances['bilgi'].insertHtml(tagHtml);
    //CKEDITOR.instances.body.insertElement(tagHtml);
  }

onclick="insertIntoCkeditor('Parakazan','Http://www.parakazan.org')"> 


To append HTML at the end you can do this:

var targetEditor = CKEDITOR.instances.idOfYourTextarea;
var range = targetEditor.createRange();
range.moveToElementEditEnd(range.root);
targetEditor.insertHtml("<p>foo</p>", 'html', range);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!