4

I know this has been asked before, but I'm trying to load some divs via jQuery (AJAX) that are attached to a tinymce instance.

In the page loaded by AJAX, there are several tinymce divs, that are created by a PHP loop;

<div id="editable_ID1"><p>Some text</p></div>
<div id="editable_ID2"><p>Some more text</p></div>

tinymce.init({
        selector: "#editable_ID1",
..............
});

tinymce.init({
        selector: "#editable_ID2",

..............
});

ID1 and ID2 are dynamically taken from the database via the PHP loop.

My problem is, I cannot seem to load the tinymce and attach it to these div elements.

I've seen:

tinyMCE.execCommand('mceAddControl', true, 'id');

But again, this doesn't seem to attach the tinymce to the elements

thanks

jQuery(document).ready(function () {

var child = jQuery(this).attr('data-id');

 $.ajax({  
  type: "POST",                                    
  url: '<?php echo url_for('@load_template_from_nav') ?>',
  data: "template_id="+child,                        

  success: function(data)          
  {
    // replace ajax page content
    $('.template-editor-item-replace').html(data);
    tinyMCE.execCommand('mceAddControl', true, 'editable_1');

  } 

 });

});
user1970557
  • 515
  • 1
  • 9
  • 23

2 Answers2

3

You need to call the ID explicitly. Use

tinyMCE.execCommand('mceAddControl', true, 'editable_ID1'); // additionaly editable_ID2
Inizio
  • 2,226
  • 15
  • 18
Thariama
  • 50,002
  • 13
  • 138
  • 166
1

When dealing with ajax that replaces the tinymce element you need to remove control first and then add it again

$.ajax({  
  type: "POST",                                    
  url: '<?php echo url_for('@load_template_from_nav') ?>',
  data: "template_id="+child,                        
  beforeSend: function(){
    tinymce.execCommand('mceRemoveControl', false, 'editable_1')
  },
  success: function(data)          
  {
    // replace ajax page content
    $('.template-editor-item-replace').html(data);
  },
  complete: function() {
     tinyMCE.execCommand('mceAddControl', true, 'editable_1');
  }
});

In tinymce v.4

$.ajax({  
  type: "POST",                                    
  url: '<?php echo url_for('@load_template_from_nav') ?>',
  data: "template_id="+child,                        
  beforeSend: function(){
    tinymce.remove('#editable_1');
  },
  success: function(data)          
  {
    // replace ajax page content
    $('.template-editor-item-replace').html(data);
  },
  complete: function() {
     tinymce.add('#editable_1');
  }
});
Inizio
  • 2,226
  • 15
  • 18
vadims
  • 147
  • 1
  • 1
  • 6