How to dynamically replace content in TinyMCE?

Peter picture Peter · Dec 14, 2011 · Viewed 28.4k times · Source

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?

Answer

Thariama picture Thariama · Dec 14, 2011

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