How do I get tinyMCE editor instance by the element selector?

You can use id in the selector and set content after the editor is initialized with the callback function:

tinymce.init({
        selector: 'textarea#tinymce1',
init_instance_callback : function(){
tinymce.get('tinymce1').setContent(data)
}
});

this is the html

<textarea id="tinymce1"></textarea>

use init_instance_callback because if you set the content with the ajax request it can goes faster than the initialization of the editor


Per the get() API the string you pass to that call needs to be the ID of the editor element not a Class.

https://www.tinymce.com/docs/api/class/tinymce/#get

So if you want to target the editor based on the ID you need something like this:

<textarea class='tiny-mce' id='editor1'></textarea>
<textarea class='tiny-mce' id='editor2'></textarea>    

... and in your JavaScript something like this...

tinymce.get('editor2').setContent('...content here...');

If you only have one editor on the page you can also use

tinymce.activeEditor.setContent('...content here...');

EDIT: It depends on when in the editor's lifecycle you want to call the get() or activeEditor methods.

These won't return anything until after TinyMCE is fully initialized. If you have code like this:

<form method="post" action="dump.php">
    <textarea class='tiny-mce' id='editor1'></textarea>
    <textarea class='tiny-mce' id='editor2'></textarea>  
</form>
<script>
    tinymce.get('editor2').setContent("content here");
</script>

This will typically fail because you don't know if TinyMCE is finished initializing the textareas when your JavaScript is running. Please see this fiddle:

http://fiddle.tinymce.com/gufaab/1

The best way to load content into the editor is to use the editor's own "init" callback and place your code in there. For example:

tinymce.init({
    selector: "textarea",
    plugins: [
        "advlist autolink lists link image charmap print preview anchor",
        "searchreplace visualblocks code fullscreen",
        "insertdatetime media table contextmenu paste"
    ],
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image",
    setup: function (editor) {
        editor.on('init', function () {
             this.setContent('The init function knows on which editor its called - this is for ' + editor.id);
        });
    } 
}); 

...note that the init gets called for each editor (if there are multiple) and you have access to the editor object in the DOM if needed.