If you do not have control over the init method from TinyMCE, you can follow this solution. This basically adds a fallback error if TinyMCE is not initialized.
jQuery(document).ready(function($) { function myCustomSetContent( id, content ) { // Check if TinyMCE is defined or not. if( typeof tinymce != "undefined" ) { var editor = tinymce.get( id ); // Check if TinyMCE is initialized properly or not. if( editor && editor instanceof tinymce.Editor ) { editor.setContent( text ); editor.save( { no_events: true } ); } else { // Fallback // If TinyMCE is not initialized then directly set the value in textarea. //TinyMCE will take up this value when it gets initialized. jQuery( '#'+id ).val( text ); } return true; } return false; } function myCustomGetContent( id ) { // Check if TinyMCE is defined or not. if( typeof tinymce != "undefined" ) { var editor = tinymce.get( id ); // Check if TinyMCE is initialized properly or not. if( editor && editor instanceof tinymce.Editor ) { return editor.getContent(); } else { // Fallback // If TinyMCE is not initialized then directly set the value in textarea. // TinyMCE will take up this value when it gets initialized. return jQuery( '#'+id ).val(); } } return ''; } $(".class-to-update-content").on("click", function(e) { myCustomSetContent( "tinymce-editor-id", "New Content in Editor" ); }); $(".class-to-get-content").on("click", function(e) { $("div.class-to-display-content").html( myCustomGetContent( "tinymce-editor-id" ) ); }); });
Link: http://blog.incognitech.in/tinymce-undefined-issue/
source share