Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically replace content in TinyMCE?

I want to replace all {baseurl} keywords to proper url in TinyMCE editor. How can i do that?

For example if user will add HTML in editor <img src="{baseurl}/image.jpg" /> i want to see this image in TinyMCE editor - so this will be replaced to <img src="http://mydomain.com /image.jpg" />

Any ideas?

like image 876
Peter Avatar asked Dec 14 '11 14:12

Peter


3 Answers

Here is the code that will replace your editor content. But you will need to do this action at the correct time.

var editor = tinymce.get('my_editor_id'); // use your own editor id here - equals the id of your textarea
var content = editor.getContent();
content = content.replace(/{\$baseurl}/g, 'http://mydomain.com');
editor.setContent(content);
like image 187
Thariama Avatar answered Oct 20 '22 15:10

Thariama


With this solution I was able to modify the content on-the-fly, without replacing the content as whole:

tinymce.init({
   setup: (editor)=>{
      editor.on('init', ()=>{
         $(editor.contentDocument).find('a').prop('title', 'my new title');
      });
   }
});

Maybe it helps someone :)

like image 28
jaheraho Avatar answered Oct 20 '22 13:10

jaheraho


I used a very simple code working good with me

tinymce.get("page-content").setContent(''); // 'page-content' as the textarea id
tinymce.get("page-content").execCommand('mceInsertContent', !1, 'New content data');
like image 27
Abdo-Host Avatar answered Oct 20 '22 13:10

Abdo-Host