Add image to TinyMCE editor


AT-2017

I am using an TinyMCEeditor to insert formatted text, and one of my requirements is to insert an image, like wrapping text on an image. So I plan to make it work another way. What I'm doing now is uploading an image using HTMLfile upload and then inserting the demo content into the editor. like this:

$(document).ready(function () {
    $('#sample img').click(function () {
       tinyMCE.activeEditor.setContent('Hello World!');
   });
}); 

So with the code above, I first upload the image, then make the image clickable, and finally insert some default text into the editor. Sample image:

Sample imageNow, I want to insert the image into the editor, so I am trying to retrieve the image source as follows, but unable to do so:

tinyMCE.activeEditor.setContent('<img src='' />');

I'm not sure, but it seems like I'm missing something here. If someone sheds some light on this, I'd appreciate it - thanks. The complete code is as follows:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
  <script>
  $(document).ready(function () {
        $('#sample img').click(function () {
             tinyMCE.activeEditor.setContent('Hello World!');
       });
  });  

 tinymce.init({ selector:'textarea' });
</script>
</head>
<body>
  <textarea></textarea>

  <span id="sample">
     <img id="blah" alt="Upload Image Here" width="100" height="100" />
  </span>

  <input type="file" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0])">
</body>
</html>
SouXin

You can modify the onchange event like this:

<input type="file" onchange="document.getElementById('blah').src = window.URL.createObjectURL(this.files[0]); 
     tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent() + '<img src=\"'+document.getElementById('blah').src + '\"/>')"/>

This should work.

Or another option is to create a function that does this:

function onChangeEvent()
{
   document.getElementById('blah').src = window.URL.createObjectURL(this.files[0]);
   tinyMCE.activeEditor.setContent(tinyMCE.activeEditor.getContent() + 
   '<img src="'+document.getElementById('blah').src + '"/>')/>
}

Then enter something like this:

<input type="file" onchange="onChangeEvent()">

Related


Add image to TinyMCE editor

AT-2017 I am using an TinyMCEeditor to insert formatted text, and one of my requirements is to insert an image, like wrapping text on an image. So I plan to make it work another way. What I'm doing now is uploading an image using HTMLfile upload and then inser

Tinymce editor image upload plugin add full image URL

username I'm using an image uploader plugin called justboil.com for the tinymce editor. It speaks fine and uploads the images to display them, but I want it to add the full web URL to the image's path, for example: * http://www.domain.com/upload_img/img1.jpg *

Tinymce editor image upload plugin add full image URL

username I'm using an image uploader plugin called justboil.com for the tinymce editor. It says great and uploads images to display them, but I want it to add the full web URL to the path to the image, like: * http://www.domain.com/upload_img/img1.jpg * instea

Tinymce editor image upload plugin add full image URL

username I'm using an image uploader plugin called justboil.com for the tinymce editor. It says great and uploads the images to display them, but I want it to add the full web URL to the image's path, like: * http://www.domain.com/upload_img/img1.jpg * instead

Dynamically add tinymce editor

Searle When I display it, I have 2 tinymce editors available on my page. Textarea is dynamically added to the page via a user control (the page is loaded). The following js/coffescript is designed to add tinymce editor to new textarea: $(document).on 'nested:f

tinymce-react insert image into tinymce editor

hello 123 This question is similar to this stackoverflow question, we want to implement a custom image library and add images to tinymice programmatically. The only difference is that instead of installing tinymice in jquery, we are using the tinymce-react plu

Add border to part of tinyMCE editor

Vincent Teyssier I would like to use the same options as word to add a border around the selected part of the editor content. Is there such an option? or any plugins doing it? A Google search for this particular question returned nothing. Vincent Teyssier Well

Add border to part of tinyMCE editor

Vincent Teyssier I would like to use the same options as word to add a border around the selected part of the editor content. Is there such an option? or any plugins doing it? A Google search for this particular question returned nothing. Vincent Teyssier Well

Display image from tinymce editor in mail

Ernesto Spain I am using the TinyMCE plugin provided by 2amigos for yii2. I need to email everything entered in the editor. As far as I've tested, it does a good job with background, title color text. But for pictures, it shows the labels like this: In the edi

Add tinymce editor to element object instead of selector

p-question 123 I have a custom element (Aurelia equivalent of web component) which creates tinymce editor. The text area cannot be selected using selectors (as any number of these custom elements can exist on the page). I need a way to initialize a tinymce ins

Add TinyMCE editor in child row of datatable

bo zhang I'm wondering if TinyMCE is able to work in child rows. In my case, each line has a subline, and I want to add a TinyMCE editor to each subline. The problem is, as shown in the screenshot, TinyMCE doesn't work when showing the child row, I only see a

How to programmatically add html tags in TinyMCE editor?

ZG101 I am using TinyMCE to edit text. When the user selects some text and presses the button, the selected part should be wrapped in <mark></mark>html tags . The reason I'm not simply adding a custom text formatting button to the tinymce editor is because I a

Add font size option to TinyMCE editor

Gita B What's the easiest way to add font size options to Plone 4's TinyMCE editor? For some reason the client has this request. In Site Settings - TinyMCE Visual Editor - Toolbar, I don't see any options related to this feature. This is how it looks in my app

Add tinymce editor to element object instead of selector

p-question 123 I have a custom element (Aurelia equivalent of web component) which creates tinymce editor. The text area cannot be selected using selectors (as any number of these custom elements can exist on the page). I need a way to initialize a tinymce ins

Add tinymce editor to element object instead of selector

pQuestions123 I have a custom element (Aurelia equivalent of web component) which creates tinymce editor. The text area cannot be selected using selectors (as any number of these custom elements can exist on the page). I need a way to initialize a tinymce inst

Add custom style formatting to tinymce editor

bounce I want to add new style formatting to tinymce editor. One way is to edit the Ip/Internal/Core/assets/tinymce/default.jsfile: ... style_formats : [ {title : 'Quote', inline : 'span', classes : 'quote'}, {title : 'Note', inline : '

Unable to add resize event to tinymce editor

Dinesh Rawat I can't add resize event to tinymce editor. it gives me:- Uncaught TypeError: tinymce.dom.Event.add is not a function What is the correct way to do it? tinymce.init({ selector: ".tinymceTextarea", width: '100%', heigh

TinyMCE add clickable button/div in editor

Rick DeVos I want to add clickable button/image/div in html editor (not navbar). However, when I use the onBeforeSetContent.add function to filter the content and add an element with an onclick tag , the tag is stripped. Since I'm using Wordpress, everything i

Add tinymce editor to element object instead of selector

p-question 123 I have a custom element (Aurelia equivalent of web component) which creates tinymce editor. The text area cannot be selected using selectors (as any number of these custom elements can exist on the page). I need a way to initialize a tinymce ins

Add tinymce editor to element object instead of selector

p-question 123 I have a custom element (Aurelia equivalent of web component) which creates tinymce editor. The text area cannot be selected using selectors (as any number of these custom elements can exist on the page). I need a way to initialize a tinymce ins

TinyMCE add clickable button/div in editor

Rick DeVos I want to add clickable button/image/div in html editor (not navbar). However, when I use the onBeforeSetContent.add function to filter the content and add an element with an onclick tag , the tag is stripped. Since I'm using Wordpress, everything i

Add class to image from tinyMCE

daugaard47 I am wondering if it is possible to add classes to images uploaded via tinyMCE. I have this string which calls description from database. <div class="articletext"><?$string = $image["description"]; Currently, to make the image responsive, I add thi

Add class to image from tinyMCE

daugaard47 I am wondering if it is possible to add classes to images uploaded via tinyMCE. I have this string which calls description from my database. <div class="articletext"><?$string = $image["description"]; Currently, to make the image responsive, I add

Add class to image from tinyMCE

daugaard47 I am wondering if it is possible to add classes to images uploaded via tinyMCE. I have this string which calls description from my database. <div class="articletext"><?$string = $image["description"]; Currently, to make the image responsive, I add

Add an image to an object in the Unity editor

Mina Michael How to add image to empty object? Which component should I add? I tried adding the "image" component, but then I don't know what to do. Brandon Lavigne Drag the desired image into the project directory, this will create a sprite from that image. T

Add an image to an object in the Unity editor

Mina Michael How to add image to empty object? Which component should I add? I tried adding the "image" component, but then I don't know what to do. Brandon Lavigne Drag the desired image into the project directory, this will create a sprite from that image. T

Add an image to an object in the Unity editor

Mina Michael How to add image to empty object? Which component should I add? I tried adding the "image" component, but then I don't know what to do. Brandon Lavigne Drag the desired image into the project directory, this will create a sprite from that image. T

Add an image to an object in the Unity editor

Mina Michael How to add image to empty object? Which component should I add? I tried adding the "image" component, but then I don't know what to do. Brandon Lavigne Drag the desired image into the project directory, this will create a sprite from that image. T