How to add text to html source in CKEditor?


I am using CKEditor in my web application. By clicking on one link, I add text to CKEditor. It is working fine. But when I open the source tab, I cannot add this text to an existing source. Can you help me, how can I do this? Thanks in advance. Sorry for my English.

+7
source share
4 answers

If you are trying to add HTML text, you can use the createFromHtml method, for example:

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

where imageSrcUrl is the location of the image, and then you can insert it into the ckeditor source as follows:

 CKEDITOR.instances.body.insertElement(imgHtml); 

There are other methods, such as insertHtml or insertText, you can check the CKEditor API for more details on this.

+7
source

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

You can paste text into ckeditor (textarea). You just need to provide a unique identifier for ckeditor (textarea), then follow the code below.

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

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

+8
source

To add 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); 
+1
source

Other sample 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')">

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')"> 
0
source

All Articles