Insert an image into a tinymce editor

Baz picture Baz · Feb 18, 2014 · Viewed 19.6k times · Source

I have a tinymce text box on my html page. Under this text box I have a table with my file system in which I can navigate to locate my images. I would like to be able to click on an image in this table and have it inserted in the timymce text box. How might I go about doing this?

Answer

user2314737 picture user2314737 · Feb 18, 2014

I've used the command

tinyMCE.execCommand('mceInsertContent', false, '<img alt="Smiley face" height="42" width="42" src="' + sr + '"/>');

to insert the image in tinyMCE after extracting the URL from the image's src.

Note that you need to pass the CSS style of the image to tinyMCE in the command.

EDIT: for Tiny MCE 4.x, the insert image code can be simplified to: tinymce.activeEditor.insertContent('<img alt="Smiley face" height="42" width="42" src="' + sr + '"/>');

Here's the demo.