Search code examples
javascriptjqueryonclickckeditor

CKeditor ( 4.3.3 ) and function ( onclick ) add HTML to editor and refreshing view


Hello just like in the subject. Im trying to insert some html code to my editor using external button. Its image html element. But i have idea how to do. I was reading some forum but there was no solution. Thank you for any help or sugestions. If there is needed editor instance please tell me how to get it.

<input type="button" onclick="addHtmlToEditor('myimage.jpg')" />

<script type="text/javascript">
CKEDITOR.replace('content_ckeditor');

function addHtmlToEditor(imgFile){
var htmltoeditor = "http://www.example.com/images/"+imgFile;
// some code inserting my html to ckeditor and update in fly :)
}
</script>

Solution

  • You are looking for the insertHtml() function. The documentation for it is here: http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertHtml .

    The Guide at the CKEditor docs site is actually very good and I highly recommend that you read it as much as you can before starting to customize CKEditor. It's a very sophisticated software. The guide is at http://docs.ckeditor.com/#!/guide

    What you want to do is create an image as a HTML string, grab an editor instance and then insert your HTML to that editor instance. Something like this should get you started (I didn't test it).

    function addHtmlToEditor(imgFile){
        var html = '<img src="http://www.example.com/images/' + imgFile + '" />';
        var editor = CKEDITOR.instances.content_ckeditor;
        editor.insertHTML(html);
    }