Peter
Peter

Reputation: 16943

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?

Upvotes: 29

Views: 38643

Answers (3)

Abdo-Host
Abdo-Host

Reputation: 4183

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');

Upvotes: 1

jaheraho
jaheraho

Reputation: 560

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 :)

Upvotes: 2

Thariama
Thariama

Reputation: 50832

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);

Upvotes: 59

Related Questions