views:

32

answers:

1

I have a page on which I have a div that I need to be able to update with several different pieces of code. I'm using an onClick event in a different div, which calls a js function to load in the appropriate code.

When I first bring up the page, the div has only one line:

<textarea name="editor1" rows="35">&lt;p&gt;Edit Area.&lt;/p&gt;</textarea>

And I use this function to get the editor on screen:

function showEditor() {
    CKEDITOR.replace( 'editor1', {height:'450'} );
}

(No, I didn't need to use a function. This was one of my attempts to fix the issue.)

It works great, as long as it gets used first.

The problem occurs after I load another set of code into the div. At that point, if I try to load in the editor again, I get an error that "editor1" is not defined. The error occurs whether or not "editor1" is actually part of the new code loaded into the div.

It seems to me the Javascript doesn't know about the new "editor1" item after a new load, and therefore can't do the replace.

I need to be able to indiscriminately load which ever routine I need into the div at any time. What do I need to do to accomplish this?

Thanks,

Sean.

+1  A: 

Try this

var myEditorInstance = null;
function showEditor() {
    if (myEditorInstance)
        {
          myEditorInstance.destroy();
         // and if you have issues with the above you can try this ;)
         //CKEDITOR.remove(myEditorInstance);
        }

    myEditorInstance = CKEDITOR.replace( 'editor1', {height:'450'} );
}
Gaby
Don't use CKEDITOR.remove because it's meant for internal use and doesn't clean up the DOM. http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.html#.remove
AlfonsoML
Gaby
In that forum thread it's explained over and over again how to use the .destroy() method.Using CKEDITOR.remove won't take care of other elements injected into the page like context-menus, dialogs, and the references to the DOM elements of the editor will still remain in the editor object, with event listeners... If you wanna have problems with your clients stating that the browser is eating their memory then go that way, or you could just as easily call the destroy() method and forget about that.
AlfonsoML
@AlfonsoML, hey hey .. do not get worked up .. Lots of people in that thread mention issues with the destroy method.. but i agree that it is the recommended way to go .. i will update my answer
Gaby
Okay, I realized that I was probably going about the problem incorrectly.I can load pages into the div with no problem, so I decided to create an html page that created a textarea and then loaded the editor. Basic stuff. So I did.It works as long as it's loaded alone as a stand alone page. But when I try to load it into the div, I get the textarea, not the editor.Any idea why?
Sean