2

How do I set text in CKEditor? CKEditor also needs to integrate with ckfinder.

I tried doing

// I need to set ckeditor text with a value in code behind. To get that value from code bhind, I am using a div which would be set in code behind. This is not hidden currently but I would do that eventually. I need to set this value to my ckeditor.

    <textarea id="editor1" name="editor1"></textarea>
        <script type="text/javascript">
            window.onload = function () {

                    var edt = CKEDITOR.replace('editor1', { toolbar: 'Basic' });
 CKFinder.setupCKEditor(edt, '/ckfinder/');

                    var t = <%=editortext.InnerText %>;


                    CKEDITOR.instances.editor1.setData(t);

            }

If I put some static text for t, var t = "Some Text";

and then set

CKEDITOR.instances.editor1.setData(t); it works fine.

If I use,

var t = <%=editortext.InnerText %>;


                    CKEDITOR.instances.editor1.setData(t);

ckeditor is no longer displayed. Only text area is displayed. How to set text in ckeditor ? Please help

user1528884
  • 121
  • 1
  • 4
  • 13

5 Answers5

9

This syntax may be useful here:

CKEDITOR.instances['editor1'].setData(t); // where editor1 is id

OR try this

edt.setData(t);
Ganesh Bhosale
  • 2,000
  • 18
  • 21
1
<script>
     function SetContents(value ) {

        var oEditor =  CKEDITOR.instances.MainContent_editor1;       
        var t = document.getElementById('<%=editor1.ClientID %>').value ;
        oEditor.setData(t); 

     } 

 </script>


<script type="text/javascript">

                var ckEditor = CKEDITOR.replace('<%=editor1.ClientID %>', {

                    //               extraPlugins: 'bbcode',
                    //               fullPage : true,
                    extraPlugins: 'docprops',
                    removeDialogTabs: 'image:advanced',
                    filebrowserImageUploadUrl: 'Upload.ashx',
                    resize_enabled: false,


                    toolbar: [

                ['Source', '-', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink'],

                ['FontSize', 'TextColor', 'BGColor'],

                ['Image']

            ]

                });
                var oEditor = CKEDITOR.instances.MainContent_editor1;
                var t = document.getElementById('<%=editor1.ClientID %>').value;
                oEditor.setData(t);

                function pageLoad() { // this is because after postback jquery not working

                            var instance = CKEDITOR.instances['<%=editor1.ClientID %>'];


                    if (instance) {
                        CKEDITOR.remove(ckEditor);
                    }
                    CKEDITOR.replace('<%=editor1.ClientID %>', {

                        //               extraPlugins: 'bbcode',
                        //               fullPage : true,
                        extraPlugins: 'docprops',
                        removeDialogTabs: 'image:advanced',
                        filebrowserImageUploadUrl: 'Upload.ashx',
                        resize_enabled: false,
                        toolbar: [

                ['Source', '-', 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink'],

                ['FontSize', 'TextColor', 'BGColor'],

                ['Image']

            ]

                    });

                    var oEditor = CKEDITOR.instances.MainContent_editor1;
                    var t = document.getElementById('<%=editor1.ClientID %>').value;
                    oEditor.setData(t);

                    }

 </script>
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48
0

Check in your browser's console for errors first. Also observe what is rendered from your backend code into this template. Most likely what you're missing are quotation marks "" and/or your rendered string contains unescaped apostrophes/quot. marks.

Console is everything.

oleq
  • 15,697
  • 1
  • 38
  • 65
0

this post is quote old but I hope I am not too late for others to see this:

You forgot to enclose the server side code with quotes:

var t = "<%=editortext.InnerText %>";

the page will be rendered like this:

var t = "your text here";

instead of

var t = your text here;

using your code will definitely break javascript's parser

Aaron
  • 41
  • 6
0

You just put double quotes

for example :-

var mata = CKEDITOR.replace('meta_des');
var editor = CKEDITOR.replace('des1');
mata.setData("meta_des; ?>");
editor.setData("description1 ;?>");

here my meta_des is ckeditor and i want past my value in that ckeditor i will just put double quotes on my php tag and simply it will print my value that comes in database it will print.