1

I am trying to dynamically create and destroy TinyMCE editors like this . 1) It appears that if I put a break point and check for tinymce editors after creation , i.e after the first for loop - I dont see them in the Browser 2) Nor do I see them in tinymce.editors in console

Hence my destroy is not working . However after everything , I can see the editors . Because the second for loop won't get executed .

Question mainly is , is there something like function has to exit and then browser makes the DOM changes ? If so should I put a sleep ? I got this idea from What is the JavaScript version of sleep()? i.e Browser makes DOM changes after the function exists or something like that . Can anyone enligthen me ?

for (var i=0;i<10;i++) 
    {
    //i=0;
    divObj = document.createElement('div');
    divObj.className = 'top_div';
    divObj.id = 'top_div_' + i;
    divObj.innerHTML = 'Tiny Editor';
    var textareaObj = document.createElement('textarea');
    textareaObj.className = 'simpleEdit';
    textareaObj.id = 'nic_' + i;
    textareaObj.style.cssText="width: 300px; height: 100px;";
    divObj.appendChild(textareaObj);
    document.body.appendChild(divObj);
    tinyMCE.execCommand('mceAddControl', false, textareaObj.id);
    }
    editors = tinyMCE.editors.length;

   for (var i=0;i<editors;i++)
   {
     tinyMCE.remove(tinyMCE.editors[0]);
    } 
Community
  • 1
  • 1
Nishant
  • 20,354
  • 18
  • 69
  • 101
  • window.setTimeout(destroy,2000) would solve this problem . TinyMCE apparently needs some time for the editors to show up in the browser , so we need to call the destroy after some time . – Nishant Apr 08 '13 at 07:32
  • this depends on your client machiene - it won't work on an old and slow PC – Thariama Apr 09 '13 at 12:42

1 Answers1

0

You are calling the remove function right after you create the editors. Thus, your editor instances have not been fully created - which is the reason for you not finding any instances. The right way here is to use the tinymce init config param onInit, which gets called when the editor is ready.

Thariama
  • 50,002
  • 13
  • 138
  • 166